2008中的条件语句

2008中的条件语句

本文介绍了SQL Server 2008中的条件语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQL Server中有一个具有以下字段的表
book_code .......书名......股票
123 ...........学习C#......... 2
222 ....................学习JAVA ... 0

我想显示以下结果
如果stock = 0则显示AVAILABLE ="YES",否则为"NO"


book_code .......书名...有库存吗?
123 ...................学习C#......... 2 ......是的
222 ....................学习JAVA ... 0 ..... NO

我了解到我可以在SQL中使用if语句,但是即使经过很多尝试,我也无法完成它.
任何帮助或信息将不胜感激
在此先感谢那些愿意提供帮助的人:)

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 :)

推荐答案

SELECT BookCode, BookTitle, Stock,
    CASE Stock
         WHEN 0 THEN 'NO'
         ELSE 'YES'
    END AS Available
FROM Books


SELECT book_code,
       [book title],
       stock,
       CASE
          WHEN stock = 0 THEN 'YES'
          ELSE 'NO'
       END AS 'AVAILABLE'
FROM   [YOURTABLE]


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



-希望对您有帮助.



- Hope this can help you.


这篇关于SQL Server 2008中的条件语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:27