T-SQL substring - separating first and last name

前端 未结 11 1742
南方客
南方客 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:54

    You may have problems if the Fullname doesn't contain a space. Assuming the whole of FullName goes to Surname if there is no space and FirstName becomes an empty string, then you can use this:

    SELECT
      RTRIM(LEFT(FullName, CHARINDEX(' ', FullName))) AS FirstName,
      SUBSTRING(FullName, CHARINDEX(' ', FullName) + 1, 8000) AS LastName
    FROM
      MyNameTable;
    

提交回复
热议问题