October 5, 2009 at 3:29 pm
Hi! in this edition of noob-weekly-problem (:-D) i need to know how can i update a row with substring from another row.
example;
row1 row2
aaaaassssssss null
aaaaasssssss1 null
aaaaasssssss2 null
I want to update data in row2 taking everything from the first 's' to the right
How can i do this ??
thanks in advance.
October 5, 2009 at 3:39 pm
Please show us what the expected results will be. A word description is okay, but some of us are more visual than others.
October 5, 2009 at 3:42 pm
igngua (10/5/2009)
Hi! in this edition of noob-weekly-problem (:-D) i need to know how can i update a row with substring from another row.example;
row1 row2
aaaaassssssss null
aaaaasssssss1 null
aaaaasssssss2 null
I want to update data in row2 taking everything from the first 's' to the right
How can i do this ??
thanks in advance.
UPDATE t SET
t.row2 = SUBSTRING(t.row1, CHARINDEX('s', t.row1), LEN(t.row1))
FROM dbo.table t
WHERE <condition if applicable>
Even though you're saying "rowXX", what I *think* you're trying to do is set one column equal to a value computed from another column in the same row.
Pro Tip: First, put the computed value into a SELECT statement (comment out my UPDATE statement and replace it with a SELECT and eliminate the assignment operators) to see what effect this will have on your data BEFORE committing the update ๐
MJM
October 5, 2009 at 3:54 pm
Lynn Pettis (10/5/2009)
Please show us what the expected results will be. A word description is okay, but some of us are more visual than others.
ok.
the output:
row1----------- row2
aaaaassssssss ssssssss
aaaaasssssss1 sssssss1
aaaaasssssss2 sssssss2
October 5, 2009 at 3:55 pm
Mark Marinovic (10/5/2009)
igngua (10/5/2009)
Hi! in this edition of noob-weekly-problem (:-D) i need to know how can i update a row with substring from another row.example;
row1 row2
aaaaassssssss null
aaaaasssssss1 null
aaaaasssssss2 null
I want to update data in row2 taking everything from the first 's' to the right
How can i do this ??
thanks in advance.
UPDATE t SET
t.row2 = SUBSTRING(t.row1, CHARINDEX('s', t.row1), LEN(t.row1))
FROM dbo.table t
WHERE <condition if applicable>
Even though you're saying "rowXX", what I *think* you're trying to do is set one column equal to a value computed from another column in the same row.
Pro Tip: First, put the computed value into a SELECT statement (comment out my UPDATE statement and replace it with a SELECT and eliminate the assignment operators) to see what effect this will have on your data BEFORE committing the update ๐
MJM
thanks iยดm going to try it.
about the pro tip; i always do that, ๐
October 5, 2009 at 10:04 pm
Then you'll like this pro tip... what you are apparently calling rows are really columns.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 6, 2009 at 9:21 am
Jeff Moden (10/5/2009)
Then you'll like this pro tip... what you are apparently calling rows are really columns.
my bad!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply