Wednesday, March 28, 2012

parse ??

how can i parse NameSurname field with t-sql ?
NameSurname
--
John SMITH
Eric Smith PHILIPS
Name Surname
-- --
John SMITH
Eric Smith PHILIPS
thanksSharkSpeed wrote:
> how can i parse NameSurname field with t-sql ?
> NameSurname
> --
> John SMITH
> Eric Smith PHILIPS
> Name Surname
> -- --
> John SMITH
> Eric Smith PHILIPS
> thanks
Here's an example:
CREATE TABLE Contacts (fullname VARCHAR(20) PRIMARY KEY)
INSERT INTO Contacts VALUES ('Bill Clinton')
INSERT INTO Contacts VALUES ('George W. Bush')
INSERT INTO Contacts VALUES ('Smith')
SELECT NULLIF(LEFT(fullname,LEN(fullname)+1 -
CHARINDEX(' ',REVERSE(' '+fullname))),'') AS firstname,
LTRIM(RIGHT(fullname,CHARINDEX(' ',REVERSE(' '+fullname)))) AS
lastname
FROM Contacts
David Portas
SQL Server MVP
--

No comments:

Post a Comment