T-SQL substring - separating first and last name

前端 未结 11 1713
南方客
南方客 2020-11-30 05:50

I have a column which has FirstName and LastName together. I\'m writing a report to separate the FirstName And LastName. How do I get the FirstName and LastName separated in

相关标签:
11条回答
  • 2020-11-30 06:04

    You could do this if firstname and surname are separated by space:

    SELECT SUBSTRING(FirstAndSurnameCol, 0, CHARINDEX(' ', FirstAndSurnameCol)) Firstname,
    SUBSTRING(FirstAndSurnameCol, CHARINDEX(' ', FirstAndSurnameCol)+1, LEN(FirstAndSurnameCol)) Surname FROM ...
    
    0 讨论(0)
  • 2020-11-30 06:07

    Assuming the FirstName is all of the characters up to the first space:

    SELECT
      SUBSTRING(username, 1, CHARINDEX(' ', username) - 1) AS FirstName,
      SUBSTRING(username, CHARINDEX(' ', username) + 1, LEN(username)) AS LastName
    FROM
      whereever
    
    0 讨论(0)
  • 2020-11-30 06:09

    The code below works with Last, First M name strings. Substitute "Name" with your name string column name. Since you have a period as a final character when there is a middle initial, you would replace the 2's with 3's in each of the lines (2, 6, and 8)- and change "RIGHT(Name, 1)" to "RIGHT(Name, 2)" in line 8.

    SELECT  SUBSTRING(Name, 1, CHARINDEX(',', Name) - 1) LastName ,
    CASE WHEN LEFT(RIGHT(Name, 2), 1) <> ' '
         THEN LTRIM(SUBSTRING(Name, CHARINDEX(',', Name) + 1, 99))
         ELSE LEFT(LTRIM(SUBSTRING(Name, CHARINDEX(',', Name) + 1, 99)),
                   LEN(LTRIM(SUBSTRING(Name, CHARINDEX(',', Name) + 1, 99)))
                   - 2)
    END FirstName ,
    CASE WHEN LEFT(RIGHT(Name, 2), 1) = ' ' THEN RIGHT(Name, 1)
         ELSE NULL
    END MiddleName
    
    0 讨论(0)
  • 2020-11-30 06:11

    I think below query will be helpful to split FirstName and LastName from FullName even if there is only FirstName. For example: 'Philip John' can be split into Philip and John. But if there is only Philip, because of the charIndex of Space is 0, it will only give you ''.

    Try the below one.

    declare @FullName varchar(100)='Philp John'
    
    Select  
        LTRIM(RTRIM(SUBSTRING(@FullName, 0, CHARINDEX(' ', @FullName+' ')))) As FirstName
    ,   LTRIM(RTRIM(SUBSTRING(@FullName, CHARINDEX(' ', @FullName+' ')+1, 8000)))As LastName
    

    Hope this will help you. :)

    0 讨论(0)
  • 2020-11-30 06:12

    This will take care of names like "Firstname Z. Lastname" and "First Z Last"

    SELECT
    CASE 
        WHEN CHARINDEX(' ',name) = 0 THEN name
        WHEN CHARINDEX(' ',name) = PATINDEX('% _[., ]%',name) THEN RTRIM(SUBSTRING(name, 1, CHARINDEX(' ',name) + 2)) 
        ELSE SUBSTRING(name,1, CHARINDEX(' ',name))
    END [firstname]
    ,CASE 
        WHEN CHARINDEX(' ',name) = 0 THEN ''
        WHEN CHARINDEX(' ',name) = PATINDEX('% _[., ]%',name) THEN LTRIM(SUBSTRING(name, CHARINDEX(' ',name) + 3,1000)) 
        ELSE SUBSTRING(name,CHARINDEX(' ',name)+1,1000)
    END [lastname]
    FROM [myTable]
    
    0 讨论(0)
提交回复
热议问题