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)
 Control[If-Else or Switch case] Statement in SQL

Author  Topic 

amitranjan
Starting Member

45 Posts

Posted - 2008-12-18 : 04:59:09
Can I use Control Statements like if else and Switch case in sql query? If yes? How?. Lets take an example i am passing param as date in sp and i m running a select query there and want to implement if-else or switch condition there.
Please guide me...It will be highly appreciated.



Thanks and regards

amit Ranjan

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-18 : 05:25:18
use switch case is not present in sql , u have to use case
case when date = @date then 1 else 0 end AS aliasname
u have follow like this for case statement
Go to Top of Page

amitranjan
Starting Member

45 Posts

Posted - 2008-12-18 : 05:55:25
Can you provide me some sort of example

amit Ranjan
Go to Top of Page

bklr
Master Smack Fu Yak Hacker

1693 Posts

Posted - 2008-12-18 : 06:05:25
DECLARE @tab table(active bit)
insert into @tab select 1 union all
select 0 union all
select 1
select * from @tab

select case when active = 1 then 'active' else 'inactive' end as status from @tab

example on case function
Go to Top of Page

Jai Krishna
Constraint Violating Yak Guru

333 Posts

Posted - 2008-12-18 : 06:25:22
Please have a look at books on line u will get plenty of examples on case statements

Jai Krishna
Go to Top of Page
   

- Advertisement -