Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 2008 sql将全名字段解析为第一、中间、最后和后缀_Sql Server 2008 - Fatal编程技术网

Sql server 2008 sql将全名字段解析为第一、中间、最后和后缀

Sql server 2008 sql将全名字段解析为第一、中间、最后和后缀,sql-server-2008,Sql Server 2008,我试着把全名分成最后、第一、中间和后缀。我搜索了一下,但找不到与我完全相同的格式。我有以下代码,但在运行完整的select时出现了此错误 Msg 537, Level 16, State 3, Line 1 Invalid length parameter passed to the LEFT or SUBSTRING function. SpaceComma表获取正确的索引。 这是我的姓名格式: CREATE TABLE #myfullnames (fullName VARCHAR(

我试着把全名分成最后、第一、中间和后缀。我搜索了一下,但找不到与我完全相同的格式。我有以下代码,但在运行完整的select时出现了此错误

Msg 537, Level 16, State 3, Line 1
Invalid length parameter passed to the LEFT or SUBSTRING function.
SpaceComma表获取正确的索引。 这是我的姓名格式:

    CREATE TABLE #myfullnames (fullName VARCHAR(50))
    GO

    INSERT #myfullnames VALUES ('BROOK SR, JAMES P.')
    INSERT #myfullnames VALUES ('BLOCK JR., BILL V.')
    INSERT #myfullnames VALUES ('MOOR, CLODE M.')
    INSERT #myfullnames VALUES ('SOUDER III, Laurence R.')
    INSERT #myfullnames VALUES ('SOUDER, WILL' )
    INSERT #myfullnames VALUES ('KOLIV, Kevin E.')
    INSERT #myfullnames VALUES ('Simk, JR. Thomas Todd')
    INSERT #myfullnames VALUES ('Polio, Gary R.')
谢谢你的帮助。谢谢

 select SplitNames.LastName, SplitNames.FirstName, 
        SplitNames.MiddleName, SplitNames.Title
 from (
    select [fullName]
, substring([fullName], 1, SpceTitle-1) as LastName
, substring([fullName], SpceMid,(SpceMid - SpceFirstName - 1)) as FirstName
, substring([fullName], SpaceComma.SpceTitle, (SpaceComma.SpceFirstName -   
    SpaceComma.SpceTitle)) as Title
, nullif(substring([fullName],SpaceComma.SpceMid+1,100),'') as    
    MiddleName      
from (
    select [fullName],
    charindex(',',[fullName]) as Comma,
    charindex(' ',[fullName]+space(1),charindex(',',[fullName])) as
            SpceFirstName,
    (len([fullName]) + 1 - charindex(' ',reverse([fullName]), 0)) as
            SpceMid,        
    charindex(' ',[fullName], charindex (' ',reverse([fullName]))) as SpceTitle
    from #myfullnames   
     ) SpaceComma
 ) SplitNames

 DROP TABLE #myfullnames

示例中的数据不遵循任何固定的规则集,因此没有解析名称的完美解决方案。规则冲突的一个例子是在Simk和BLOCK之间,JR位于其中一个的逗号内,而不是另一个的逗号内。违反规则的唯一解决方案是手动纠正违规者

我们可以使用SQLServer中的PARSENAME函数解析名称。SQL Server使用PARSENAME来分隔SERVERNAME.DATABASE.SCHEMA.TABLE,并且限制为四个部分

下面是一个解析名称的查询

select fullname
 , REPLACE(fullname,'.','') AS [1] 
 , REPLACE(REPLACE(fullname,'.',''),', ','.') AS [2]
 , ParseName(REPLACE(REPLACE(fullname,'.',''),', ','.'),2) AS [3]
 , REPLACE(ParseName(REPLACE(REPLACE(fullname,'.',''),', ','.'),1),' ','.') AS [4]
 , PARSENAME(REPLACE(ParseName(REPLACE(REPLACE(fullname,'.',''),', ','.'),1),' ','.'),1) AS [5]
 , PARSENAME(REPLACE(ParseName(REPLACE(REPLACE(fullname,'.',''),', ','.'),1),' ','.'),2) AS [6]
 from #myfullnames
六个输出列演示了如何使用替换字符。然后使用PARSENAME提取字符串的一部分