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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 update only if record isn't blank

Author  Topic 

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-15 : 03:29:36
i have an update statemetn where i update one table based on another

UPDATE t1
SET
t1.number=ISNULL(vwt1access.number,t1.number),
t1.name=ISNULL(vwt1access.naeme,t1.name),
t1.smsreceipt=ISNULL(vwt1access.smsreceipt,t1.smsreceipt),

t1.[datetime]=getdate()
FROM
vwt1access




what do i need to change if I want it to update the field only if the new field is not blank or null ?? (meaning if it's blank I want to keep the old data) - now it blanks it

Kristen
Test

22859 Posts

Posted - 2010-02-15 : 03:55:26
[code]
t1.name=ISNULL(NullIf(vwt1access.name, ''),t1.name),
[/code]
Go to Top of Page

esthera
Master Smack Fu Yak Hacker

1410 Posts

Posted - 2010-02-15 : 03:57:29
thanks kristen

this will also not update any blanks in additional to null?
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-15 : 04:00:33
Yup
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-15 : 08:04:20
quote:
Originally posted by esthera

i have an update statemetn where i update one table based on another

UPDATE t1
SET
t1.number=ISNULL(vwt1access.number,t1.number),
t1.name=ISNULL(vwt1access.naeme,t1.name),
t1.smsreceipt=ISNULL(vwt1access.smsreceipt,t1.smsreceipt),

t1.[datetime]=getdate()
FROM
vwt1access




what do i need to change if I want it to update the field only if the new field is not blank or null ?? (meaning if it's blank I want to keep the old data) - now it blanks it


whats t1? is this the full query? I also cant spot any relationships between tables

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

Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-02-15 : 08:47:38
Yeah, typos in column names too, I figured it was just a "sample example"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-15 : 08:48:58
quote:
Originally posted by Kristen

Yeah, typos in column names too, I figured it was just a "sample example"


Me too hoping so otherwise posted code does not make any sense at all

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

Go to Top of Page
   

- Advertisement -