Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Append new text to a column

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2012-10-02 : 11:04:42
Hi,

I have a need to append a text string into a column of nvarchar.
Something like next:

Update my_table
set my_Col = select my_col from my_table where id = 123 + "new text"
where id = 123

But I don't think I get the syntax right.

Also, this code is called from app side. Is there a way to not select the existing value to save a round trip?

Thanks!

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-10-02 : 11:15:15
The correct syntax would be this:
Update my_table 
set my_Col = my_col + 'new text'
where id = 123
It has no "SELECT", so there is no roundtrip.

If there is nothing in my_col (i.e., if it is null), for it to work correctly, you also need to do one more thing, see below:
Update my_table 
set my_Col = COALESCE(my_col,'') + 'new text'
where id = 123
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-10-02 : 11:35:35
also make sure my_Col has sufficient length to hold the new appended string as well

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2012-10-02 : 11:41:41
THANKS! You guys are awesome!
Go to Top of Page
   

- Advertisement -