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
 Removing condition Where

Author  Topic 

j0shua
Starting Member

40 Posts

Posted - 2009-01-29 : 05:22:40
Hi, my code is like this


if column3 = 1
BEGIN
SELECT columm1,
column2,
column3
FROM table1
WHERE column1 = column2
END
ELSE
SELECT columm1,
column2,
column3
FROM table1
END



As you notice, the only difference is the where statement
My actual code contains 10 repetitions of this depending on the category the user chose that is why i want to shorten this.

My Question is: is there a way to shorten this?

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-29 : 05:25:23
try this

SELECT columm1,
column2,
column3
FROM table1
WHERE ( column3 = 1 AND (column1 = column2)) OR column3 <> 1
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-29 : 05:26:09
[code]SELECT Col1,
Col2,
Col3
FROM Table1
WHERE (Col1 = Col2 AND Col3 = 1)
OR Col3 <> 1[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

j0shua
Starting Member

40 Posts

Posted - 2009-01-29 : 06:12:19
thank you!! everything is clear to me now!
Go to Top of Page

raky
Aged Yak Warrior

767 Posts

Posted - 2009-01-29 : 06:15:44
Welcome...
Go to Top of Page
   

- Advertisement -