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 2008 Forums
 Transact-SQL (2008)
 If Column is null

Author  Topic 

ronjohn
Starting Member

4 Posts

Posted - 2010-07-31 : 18:51:05
Can somebody tell me if I can do the following in sql:

If (Column is null)
Do something

Thanks,


pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-08-01 : 02:42:23
Yes it can be done.

Example for you:

declare @Test varchar(50)
--Default value is null for @Test

if @Test is null
Begin
Print '@Test is null'
--statements to be executed.
End
Else
Begin
Print '@Test is not null'
--statements to be executed.
End

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

ronjohn
Starting Member

4 Posts

Posted - 2010-08-01 : 20:50:18
I've tried the following code and sql is telling me that rows are affected but when i look into the database all affected fields are still null.
Here's the code:

update Contacts
set Title = ' '
where ListID = '9' and Fax is null
Go to Top of Page

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2010-08-01 : 21:56:27
you've updated TITLE based on FAX being null.

Fax will still be null.

If that doesn't help, give us a few sample records and the table definition, and what you hope to accomplish
Go to Top of Page
   

- Advertisement -