Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a table in SQL server with following fields
book_code ....... book title...... stock
123 ...................learn C#.........2
222....................learn JAVA......0

i want to show following result
if stock =0 show AVAILABLE="YES" else "NO"


book_code ....... book title...... stock..available?
123 ...................learn C#.........2......YES
222....................learn JAVA......0 .....NO

i have learnt that i can use if statements in SQL but even after trying a lot i cant get it done.
any help or information will be appreciated
thanks in advance to those who are willing to help :)
Posted

Use the CASE statement

SQL
SELECT BookCode, BookTitle, Stock, 
    CASE Stock 
         WHEN 0 THEN 'NO' 
         ELSE 'YES' 
    END AS Available
FROM Books
 
Share this answer
 
Comments
greatreddevil 15-Jul-11 1:14am    
thanks shameel. :)
[no name] 15-Jul-11 3:04am    
you're welcome.
One common keyword that you can use is the CASE[^]. An example on how to use it can be seen on the link. You can also check my sample SQL script below.

SQL
SELECT book_code, 
       [book title], 
       stock,
       CASE 
          WHEN stock = 0 THEN 'YES'
          ELSE 'NO'
       END AS 'AVAILABLE'
FROM   [YOURTABLE]
 
Share this answer
 
v2
Using Case you can solve your problem

SELECT Stock, 
    CASE WHEN 
      Stock='0' Then 'YES' 
      ELSE 'NO' 
    END AS AVAILABLE
FROM Table1


- Hope this can help you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900