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
 Update one field by adding field1 + field2, etc.

Author  Topic 

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2014-10-08 : 22:11:09
I am trying to do the following query...
UPDATE [RA].[dbo].[MailingLabels]
SET [Type] = [City] + ' ' + [Day] + ' ' + [Time]
WHERE [Type] IS NULL


This works just fine unless one of the fields is null, then it doesn't update anything. How can I do something like this and have it update even if day or time is null?

Thanks,
Stacy

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2014-10-08 : 22:31:09
use ISNULL() or COALESCE()

SET [Type] = [City] + ' ' + ISNULL([Day] + ' ', '') + ISNULL([Time], '')


what is the data type for [Day] and [Time] ? if it is not string, you should use CONVERT() to convert to string


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

StacyOW
Yak Posting Veteran

93 Posts

Posted - 2014-10-09 : 22:03:44
Thanks khtan. It worked perfectly.

My fields are nvarchar.

Thanks for your help. I remember trying to do this a long time ago and I wasn't able to get the COALESCE to work.

Stacy
Go to Top of Page
   

- Advertisement -