Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
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.
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 = 123But 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
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 MVPhttp://visakhm.blogspot.com/