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 T-SQL?
相关问题
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
- Get path of node in tree
- How to evaluate an input in the WHERE clause
- Delete all records in table which have no referenc
相关文章
- How to truncate seconds in TSQL?
- Code for inserting data into SQL Server database u
- SQL Server 2008 Change Data Capture, who made the
- How do we alias a Sql Server instance name used in
- SQL identity (1,1) starting at 0
- How to do a UNION on a single table?
- SQL Group by Count of Counts
- SQL Server drop and recreate indexes of a table
Here is a more elaborated solution with a SQL function:
GetFirstname
GetLastName
USE:
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:
The easiest way I can find to do it is:
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.
Hope this will help you. :)
This should work:
Edit: Adopted Aaron's and Jonny's hint with the fixed length of 8000 to avoid unnecessary calculations.
Assuming the
FirstName
is all of the characters up to the first space: