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
 syntax help with if

Author  Topic 

albertkohl
Aged Yak Warrior

740 Posts

Posted - 2008-09-19 : 14:26:32
hey, i want to write a select query, that basically checks a field, if that field is empty, then it does one thing, if it's not, it does another... here's what i tried, but i get syntax errors... can u guys give me a hand?



select
per1_fname,per1_lname,name,address1,address2,city,
state_abbrv,zip,phone
into #work
from test.dbo.condnc c
where
(if dispo2= ' ' dispo1='1')
(if dispo2<>' ' dispo2='1')
go



do know if this is something i can do, or if i'm just being a nub... thanks!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-09-19 : 14:34:30
Here's one way:

where 1 = case when dispo2 != ' ' then dispo2 else dispo1 end

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-19 : 14:34:46
do like below:-
select
per1_fname,per1_lname,name,address1,address2,city,
state_abbrv,zip,phone
into #work
from test.dbo.condnc c
where coalesce(nullif(dispo2,' '),dispo1)='1'
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-09-19 : 16:40:05
Just use boolean logic:

where (dispo2= ' ' and dispo1='1') or (dispo2<>' ' and dispo2='1')

See http://weblogs.sqlteam.com/jeffs/archive/2003/11/14/513.aspx for more on this.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2008-09-19 : 17:57:54
Jeff,

I like that boolean logic.

I wonder: Is there a way to use it in this situation?
SELECT     
Serial_Number AS 'SN',
CASE WHEN CoilType is NULL Then ' ' Else CoilType END AS 'CoilType',
Op_ID AS 'OperatorID',
System_ID AS 'SystemID',
FROM dbo.Parts
WHERE ([Serial_Number] Like '%' + @SN + '%')



Avoid Sears Home Improvement
Go to Top of Page

LoztInSpace
Aged Yak Warrior

940 Posts

Posted - 2008-09-19 : 20:42:12
What are you talking about?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-09-20 : 01:31:57
quote:
Originally posted by jp2code

Jeff,

I like that boolean logic.

I wonder: Is there a way to use it in this situation?
SELECT     
Serial_Number AS 'SN',
CASE WHEN CoilType is NULL Then ' ' Else CoilType END AS 'CoilType',
Op_ID AS 'OperatorID',
System_ID AS 'SystemID',
FROM dbo.Parts
WHERE ([Serial_Number] Like '%' + @SN + '%')



Avoid Sears Home Improvement


didnt understand what condition you're trying to apply boolean logic. here you have only a single WHERE condition.
Go to Top of Page
   

- Advertisement -