Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'IF' statement within 'SELECT' statement 1

Status
Not open for further replies.

yuben

Programmer
Feb 8, 2001
20
ZA
How do I embed an IF statement within a SELECT statement?

I want to select values from a table using a view or stored procedure but one of the columns values must be based on the value of another column

e.g.

col1 col2 col3
1 10 12
2 15 13
3 20 14
4 25 15

SELECT col1,col2,
if col3 > 13
val='LargeVal'
else
val = 'SmallVal' as Calculated Column

I want the output to be

col1 col2 Calculated Column
1 10 SmallVal
2 15 SmallVal
3 20 LargeVal
4 25 LargeVal

i.e. Calulated column is a derived column. In MSAccess I was able to use the IIF function but SQL Server does not seem to recognise it even though the Books Online documents it.

Your help is appreciated.

 
SELECT CO1,COL2,
CASE col3
WHEN > 13
THEN 'largeval'
ELSE 'smallval'
END
FROM MyTable
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top