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)
 condition in sql - case or if statement

Author  Topic 

kiri
Starting Member

38 Posts

Posted - 2008-02-05 : 16:45:27
i have one condition in where clause like

where A.SOURCE IN ('PAR/CH/TEA','LATINO/PTC','LATINOS-B','LATINOS-E','LATINOS-S','LATINS/PTC','LATINB/PTC')

instead of where clause i have to use in condition - using if or case
statement..

i want like this:-
source = case sourceid
when o then ''
when 1 then ('PAR/CH/TEA','LATINO/PTC','LATINOS-B','LATINOS-E','LATINOS-S','LATINS/PTC','LATINB/PTC')

i can't use like IN statement in case-when and then...
it gives me error so can u tell me correct solution plz..

thanks

jdaman
Constraint Violating Yak Guru

354 Posts

Posted - 2008-02-05 : 17:06:46
If im understanding you correctly you want to have a dynamic WHERE clause where depending on the value in sourceid it will filter records by varying criteria. You can do this by using an OR statement.

where sourceid = 0 and source = ''
or sourceid = 1 and source IN ('PAR/CH/TEA','LATINO/PTC','LATINOS-B','LATINOS-E','LATINOS-S','LATINS/PTC','LATINB/PTC')

Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2008-02-05 : 17:28:59
be sure to add parenthesis to the right palces since AND has priority over OR

cond1 AND cond2 OR cond3
is different than
cond1 AND (cond2 OR cond3)


_______________________________________________
Causing trouble since 1980
blog: http://weblogs.sqlteam.com/mladenp
SSMS Add-in that does a few things: www.ssmstoolspack.com <- new version out
Go to Top of Page
   

- Advertisement -