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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

_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
_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
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

_agx_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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AleksAuthor Commented:
Worked great! Thx.
0
_agx_Commented:
Glad it helped!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.