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
 IF condition

Author  Topic 

Chinni
Yak Posting Veteran

95 Posts

Posted - 2008-10-02 : 11:44:13
Need to check if the field has space and null

if (select column from table) not (null and ' ')
print 'value exists'


how to write IF condition for this??

Thanks

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 12:06:36
[code]SELECT CASE WHEN NULLIF(column,' ') IS NOT NULL THEN 'Value Exists' END FROM table[/code]
Go to Top of Page

Chinni
Yak Posting Veteran

95 Posts

Posted - 2008-10-02 : 12:11:07
I tried like

declare @var varchar (20)

set @var = (SELECT column from table )
select @var
IF (@var is null) or (@var = '')
PRINT 'space'
else
print 'no space'



IS this correct way to do????
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-10-02 : 12:19:08
will this return a single value?else you've to use CASE WHEN

SELECT column from table
Go to Top of Page

Chinni
Yak Posting Veteran

95 Posts

Posted - 2008-10-02 : 12:28:55
yes that returns a single value

select distinct column from table where id = 1234
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-10-03 : 02:20:49
quote:
Originally posted by visakh16

SELECT CASE WHEN NULLIF(column,' ') IS NOT NULL THEN  'Value Exists' END FROM table



It can also be written

SELECT 'Value Exists' FROM table
WHERE COLUMN>''


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -