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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Append new text to a column
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Hommer
Aged Yak Warrior

808 Posts

Posted - 10/02/2012 :  11:04:42  Show Profile  Reply with Quote
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
Flowing Fount of Yak Knowledge

5155 Posts

Posted - 10/02/2012 :  11:15:15  Show Profile  Reply with Quote
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

India
52326 Posts

Posted - 10/02/2012 :  11:35:35  Show Profile  Reply with Quote
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 - 10/02/2012 :  11:41:41  Show Profile  Reply with Quote
THANKS! You guys are awesome!
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000