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.
Author |
Topic |
jgn1013
Starting Member
24 Posts |
Posted - 2008-09-22 : 11:03:13
|
I'm trying to add an IF to the end of a select query.examplesenario # 1 catselected = true and stateselected = true(select somedata from sampletable where cat='active and state='ga')senario #2 catselected = false and stateselected = true(select somedata from sampletable where state='ga')senario #3 catselected = true and stateselected = false(select somedata from sampletable where cat='active')senario #4 catselected = false and stateselected = false(select somedata from sampletable )**************************************************************declare @catslected bitdeclare @stateselected bitdecalere @allwords bitset @allwords = 0set @catselected = trueset @stateselected = falseIF @allwords = 0select name, address, description from sampletablewhereIF catselected = 1 AND stateselected = 1 cat='active' and state='ga'IF catselected = 0 AND state = 1 state='ga'IF cateselected = 1 AND sate = 0 cat='active'IF catselected = 0 and state = 0 /*get all with no restrictions*/Thanks |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-09-22 : 11:13:04
|
this is what you want i guessselect somedata from sampletable where ((cat='active' and catselected = true) or catselected = false)and ((state='ga' and stateselected = true) or stateselected =false) |
 |
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-09-22 : 11:28:03
|
[code]SELECT Name, Address, DescriptionFROM SampleTableWHERE (@CatSelected = 0 OR Cat = 'Active') AND (@StateSelected = 0 OR State = 'GA')[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
|
|