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 2000 Forums
 SQL Server Administration (2000)
 sql query using if else

Author  Topic 

Keshaba
Yak Posting Veteran

52 Posts

Posted - 2008-12-15 : 08:45:42
Hi I am using sqlserver 2005 express .I want to know how shall i write a query using if else .Is it possible to do so using sql.
I want the query to be like this If marks>75 then result = p else result = f.
suppose marks and result are field name in a table.

Keshab

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-12-15 : 09:03:04
SELECT *, CASE WHEN Marks > 75 THEN 'p' ELSE 'f' END AS Result
FROM Table1



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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 09:05:34
you should be using case

result = case when marks>75 then p else f end
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 09:05:51
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-12-15 : 09:44:25
quote:
Originally posted by visakh16

you should be using case

result = case when marks>75 then p else f end


You missed single quotes around p and f

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-15 : 10:35:06
quote:
Originally posted by madhivanan

quote:
Originally posted by visakh16

you should be using case

result = case when marks>75 then p else f end


You missed single quotes around p and f

Madhivanan

Failing to plan is Planning to fail


what if they are columns and not string values 'p' & 'f'

atleast i thought so
Go to Top of Page
   

- Advertisement -