SQLTeam.com | Weblogs | Forums

Truncate a character chain and copy it to another column


#1

Hello,

I need to truncate a chain of character from one column and copy it to another column.
Ex :
In column A, I have : Value1 _ Value2
Final result required : Value 1 in column A and Value 2 (without the separator '_' ) in column B
Both columns belong to the same table.

Could you help pls?

Thank you


#2
UPDATE YourTable SET
	columnA = LEFT(columnA, CHARINDEX('_',columnA+'_')-1),
	columnB = STUFF(columnA,1,CHARINDEX('_',columnA+'_'),'');

Since this is destructive - i.e., you are losing the orignal value in ColumnA, you might want to test in a dev environment before doing it where it really matters.


#3

Thank you JamesK very much for your help