Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Need Help TSQL Code. Expand / Collapse
Author
Message
Posted Tuesday, July 30, 2013 2:54 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:15 AM
Points: 207, Visits: 750
I have source file with column

Full Name
Jim k Smith
Robert K Lin
Chris Tim

and My destination has

FirstName and LastName

I want to parse FullName

I know how I can use SSIS Expression to parse the address, Here it is

FirstName = LTRIM(RTRIM(TRIM(REVERSE(SUBSTRING(REVERSE([Full Name]),1,FINDSTRING(REVERSE([Full Name])," ",1))))))
LastName = LTRIM(RTRIM(TRIM(SUBSTRING([Full Name],1,FINDSTRING([Full Name]," ",1)))))

Is any one can help me to transfer in T-SQL?

Thank You in advance.
Post #1479171
Posted Tuesday, July 30, 2013 3:26 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 3,476, Visits: 7,518
You need to realize that you might have names that can change the logic. The most common example would be spanish names (which can appear even in an English speaking country).
However, for the examples you posted, here's an option. Check out that the CTE is just to work with the sample data. Please post it in a consumable format next time.

WITH CTE AS(
SELECT 'Jim k Smith' [FullName] UNION ALL
SELECT 'Robert K Lin' UNION ALL
SELECT 'Chris Tim'
)
SELECT LEFT( FullName, LEN(FullName) - CHARINDEX( ' ', REVERSE(FullName))),
RIGHT( FullName, CHARINDEX( ' ', REVERSE(FullName)) - 1)
FROM CTE




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1479187
Posted Tuesday, July 30, 2013 3:34 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 8:00 AM
Points: 169, Visits: 493
I think this is a slightly shorter solution.

LastName =  reverse(left(reverse(FullName), charindex(' ', reverse(FullName)) - 1)) 

Post #1479194
Posted Tuesday, July 30, 2013 3:34 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:15 AM
Points: 207, Visits: 750
Thank You For your help...

However I want only FirstName and LastName
the logic would be for FirstName = From Left to ' ' (First Space)
the logic would be for LastName = From Rifh to ' ' (First Space)

Please advise...
Post #1479195
Posted Tuesday, July 30, 2013 3:41 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 9:09 AM
Points: 36,941, Visits: 31,443
geoff5 (7/30/2013)
I think this is a slightly shorter solution.

LastName =  reverse(left(reverse(FullName), charindex(' ', reverse(FullName)) - 1)) 



How is that shorter and why do you believe that 3 REVERSEs are better than 1?


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1479198
Posted Tuesday, July 30, 2013 3:44 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 3,476, Visits: 7,518
Then it's a lot easier.

WITH CTE AS(
SELECT 'Jim k Smith' [FullName] UNION ALL
SELECT 'Robert K Lin' UNION ALL
SELECT 'Chris Tim'
)
SELECT LEFT( FullName, CHARINDEX( ' ', FullName)),
RIGHT( FullName, CHARINDEX( ' ', REVERSE(FullName)))
FROM CTE




Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1479199
Posted Tuesday, July 30, 2013 3:54 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Today @ 9:15 AM
Points: 207, Visits: 750
Awesome,

That's what I want...

Thank You!
Post #1479208
Posted Tuesday, July 30, 2013 7:43 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Tuesday, August 12, 2014 8:00 AM
Points: 169, Visits: 493
Jeff,

I was mistaken because I looked too quickly over the code and didn't read it properly. My code was neither shorter nor simpler.

I am appropriately abashed.

Geoff
Post #1479254
Posted Thursday, August 1, 2013 1:04 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 7:18 AM
Points: 593, Visits: 1,081
HI Luis, I am a newbie and I have a doubt on your code.

What is the purpose of the REVERSE() in your code?

ie. here... REVERSE(FullName)
Post #1479806
Posted Thursday, August 1, 2013 8:49 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:43 AM
Points: 3,476, Visits: 7,518
Hi George,
You should really be trying to figure out this yourself by using BooksOnLine (BOL or SQL Server help) but I'll try to explain.
REVERSE will do exactly what it says, returns the reverse order of a string value.
I use it because I need to find the last space in the string. As CHARINDEX won't have a backwards function, I need to reverse the string to get the last space that will become the first one. Was I clear or did I confuse you even more?



Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1479983
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse