SQL to separate last name from first name

I have a table called 'cases', one of the fields is a field called 'Name', it includes the full name of the person.
I need to separate the name and move the first name to the "FirstNm" field and the last name into the "LastNm" field

How can I run a query so that it will remove any blank characters (spaces) before the first word, then the first word is the "FirstNm" and everything after the first word (space), will be the last name, the last name may include spaces, such as "De La Praga"

So that this name:  Ana De La Praga  (Name), would be separated as:
FirstNm:  Ana
LastNm:  De La Praga

Table Name: cases

Helps is greatly appreciated.
LVL 1
AleksAsked:
Who is Participating?
 
_agx_Connect With a Mentor Commented:
Update: Trimmed trailing space after "FirstNm"

If you are using SQL Server, please try the previous example.  

The original example uses a temporary @table variable, so you can test it out first, but ... here's the UPDATE only with your table name "cases",  The SQL uses a CTE because the original question said you wanted to trim spaces before the first name too:

(Always backup your table before any mass updates of course).

;WITH tbl AS
(
   SELECT 
      LTRIM(RTRIM(Name)) AS Name
      , FirstNm
      , LastNm
      , CHARINDEX(' ', LTRIM(RTRIM(Name))) AS SpacePos
   FROM   cases
)
UPDATE tbl
SET   FirstNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, 1, SpacePos-1) ELSE Name END
         , LastNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, SpacePos+1, LEN(Name)-SpacePos) ELSE NULL END

Open in new window

1
 
_agx_Commented:
If you search the archives, you'll find a lot of topics on it, such as this one.  Keep in mind separating based on spaces doesn't properly handle edge cases like "FirstName Middle LastName". Not sure of your dbms, but for SQL Server, try


-- sample table
DECLARE @YourTable TABLE
(
id integer identity
, Name nvarchar(200)
, FirstNm nvarchar(100)
, LastNm nvarchar(100)
)
-- sample data
INSERT INTO @YourTable (Name)
VALUES 
(' Ana De La Praga')
, ('Joe Miller')
, ('Ronald D McDonald')

-- update
;WITH tbl AS
(
	SELECT LTRIM(RTRIM(Name)) AS Name
			, FirstNm
			, LastNm
			, CHARINDEX(' ', LTRIM(RTRIM(Name))) AS SpacePos
	FROM   @YourTable
)
UPDATE tbl
SET    FirstNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, 1, SpacePos) ELSE Name END
		, LastNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, SpacePos+1, LEN(Name)-SpacePos) ELSE NULL END

-- display results
;SELECT * FROM @YourTable

Open in new window

0
 
AleksAuthor Commented:
Can we please us the code with the table and field names described above?   I am not interested in the Middle Name.
The first word as explained is the FirstNm, then a space, anything after that space is LastNm
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
_agx_Commented:
It does use the 3 field names you mentioned:   Name, FirstNm and LastNm.  If you are using SQL Server, please try the previous example.  

,,,,
UPDATE tbl
SET    FirstNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, 1, SpacePos) ELSE Name END
            , LastNm = CASE WHEN SpacePos > 1 THEN SUBSTRING(Name, SpacePos+1, LEN(Name)-SpacePos) ELSE NULL END
...
0
 
AleksAuthor Commented:
Worked great! Thx.
0
 
_agx_Commented:
Glad it helped!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.