Saturday, May 4, 2013

How do you perform an IF…THEN in an SQL SELECT ?

How do you perform an IF…THEN in an SQL SELECT ?

How do we perform an IF...THEN in an SQL SELECT statement?

For example : 

SELECT IF(Obsolete = 'N' or InStock = 'Y';1;0) as Salable, * FROM Product

The CASE statement is the closest to IF in SQL and is supported on all versions of SQL Server

SELECT CAST( CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END AS bit) as Salable, * FROM Product

You only need to do the CAST if you want the result as a boolean value, if you are happy with an int, this works:

SELECT CASE WHEN Obsolete = 'N' or InStock = 'Y' THEN 1 ELSE 0 END as Salable, * FROM Product

CASE statements can be embedded in other CASE statements and even included in aggregates.

SQL Server Denali adds the IIF statement which is also available in access: 

SELECT IIF(Obsolete = 'N' or InStock = 'Y', 1, 0) as Selable, * from Product

Hope you have enjoyed to read this post,
let me know if you have more suitable suggestions.

you might be more interested in SQL, SQL Server, database

Good Day [/ Night] ! Happy Google + ing !




  1. The future of PHP web developing company should be good level compared to other fields.Because Websites are very important things for business purposes.That that type websites would be create by only that companies.
    Bangalore Website Designing Companies

  2. Now a days ecommerce business became huge growth because of online customers.People wants to buy all things through online.Accordingly more ecommerce websites are evolving everyday.
    Web Design Companies in Coimbatore |Web Design Company in Coimbatore


Any Questions or Suggestions ?


Professional & Experienced Freelance Developer From India, Technologist, Software Engineer, internet marketer and Open Sources Developer with experience in Finance, Telecoms and the Media. Contact Me for freelancing projects.

Enter your email address:

Delivered by FeedBurner

PHP Freelancer India - Google+