有关SQL条件判断语句的二个例子

发布时间:2020-08-14编辑:脚本学堂
本文介绍下,sql server中有关条件判断语句的二个例子,供大家学习参考。

例1:
 

复制代码 代码示例:
SELECT
 CASE
 WHEN price IS NULL THEN 'Not yet priced'
 WHEN price < 10 THEN 'Very Reasonable Title'
 WHEN price >= 10 and price < 20 THEN 'Coffee Table Title'
 ELSE 'Expensive book!'
 END AS "Price Category",
CONVERT(varchar(20), title) AS "Shortened Title"
FROM pubs.dbo.titles
ORDER BY price

例2,
 

复制代码 代码示例:
SELECT au_fname, au_lname,
   CASE state
      WHEN 'CA' THEN 'California'
      WHEN 'KS' THEN 'Kansas'
      WHEN 'TN' THEN 'Tennessee'
      WHEN 'OR' THEN 'Oregon'
      WHEN 'MI' THEN 'Michigan'
      WHEN 'IN' THEN 'Indiana'
      WHEN 'MD' THEN 'Maryland'
      WHEN 'UT' THEN 'Utah'
        END AS StateName
FROM pubs.dbo.authors
ORDER BY au_lname

例子都很简单,仅供初学的朋友参考,不解释。