T-SQL substring - separating first and last name

前端 未结 11 1743
南方客
南方客 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 05:58

    validate last name is blank
    
    SELECT  
    person.fullName,
    (CASE WHEN 0 = CHARINDEX(' ', person.fullName) 
        then  person.fullName 
        ELSE SUBSTRING(person.fullName, 1, CHARINDEX(' ', person.fullName)) end) as first_name,  
    (CASE WHEN 0 = CHARINDEX(' ', person.fullName) 
        THEN ''  
        ELSE SUBSTRING(person.fullName,CHARINDEX(' ', person.fullName), LEN(person.fullName) )end) last_name
    
    FROM person
    

提交回复
热议问题