1、ISNULL   
  使用指定的替换值替换   NULL。   
    
  语法   
  ISNULL   (   check_expression   ,   replacement_value   )     
    
  参数   
  check_expression   
    
  将被检查是否为   NULL的表达式。check_expression   可以是任何类型的。   
    
  replacement_value   
    
  在   check_expression   为   NULL时将返回的表达式。replacement_value   必须与   check_expresssion   具有相同的类型。     
    
  返回类型   
  返回与   check_expression   相同的类型。   
    
  注释   
  如果   check_expression   不为   NULL,那么返回该表达式的值;否则返回   replacement_value。   
    
  示例   
  A.   将   ISNULL   与   AVG   一起使用   
  下面的示例查找所有书的平均价格,用值   $10.00   替换   titles   表的   price   列中的所有   NULL   条目。   
    
  USE   pubs   
  GO   
  SELECT   AVG(ISNULL(price,   $10.00))   
  FROM   titles   
  GO   
    
  下面是结果集:   
    
  --------------------------     
  14.24                                               
    
  (1   row(s)   affected)   
    
  B.   使用   ISNULL   
  下面的示例为   titles   表中的所有书选择书名、类型及价格。如果一个书名的价格是   NULL,那么在结果集中显示的价格为   0.00。   
    
  USE   pubs   
  GO   
  SELECT   SUBSTRING(title,   1,   15)   AS   Title,   type   AS   Type,     
        ISNULL(price,   0.00)   AS   Price   
  FROM   titles   
  GO   
    
  下面是结果集:   
    
  Title                       Type                   Price                       
  ---------------   ------------   --------------------------     
  The   Busy   Execut   business           19.99                                               
  Cooking   with   Co   business           11.95                                               
  You   Can   Combat     business           2.99                                                 
  Straight   Talk   A   business           19.99                                               
  Silicon   Valley     mod_cook           19.99                                               
  The   Gourmet   Mic   mod_cook           2.99                                                 
  The   Psychology     UNDECIDED         0.00                                                 
  But   Is   It   User     popular_comp   22.95                                               
  Secrets   of   Sili   popular_comp   20.00                                               
  Net   Etiquette       popular_comp   0.00                                                 
  Computer   Phobic   psychology       21.59                                               
  Is   Anger   the   En   psychology       10.95                                               
  Life   Without   Fe   psychology       7.00                                                 
  Prolonged   Data     psychology       19.99                                               
  Emotional   Secur   psychology       7.99                                                 
  Onions,   Leeks,     trad_cook         20.95                                               
  Fifty   Years   in     trad_cook         11.95                                               
  Sushi,   Anyone?     trad_cook         14.99                                               
    
  (18   row(s)   affected)   
 

2、项目中,联系人详细信息这一个页面中,联系人的性别在数据库字段的保存,男的用0表示,女的用1表示,所以,如果直接就读出来绑定到DataList中,性别就显示成0或1了。所以需要将Sql语句进行改造。先看看我原先的做法,这样做,组长叫我重做了,要叫我要CASE WHEN来写。
sql IsNull 和 case when
sql IsNull 和 case when    不用CASE WHEN 的做法。源代码如下:
sql IsNull 和 case when
sql IsNull 和 case whenstring sql
="select * from CONTACTPERSON ,CUSTOMER where CONTACTPERSONID='"+strContactid+"' and CONTACTPERSON.CUSTOMERID=CUSTOMER.CUSTOMERID";
sql IsNull 和 case whenDataSet ds
=new DataSet();
sql IsNull 和 case whends
=Common.GetDSCommon(sql);   
sql IsNull 和 case when
sql IsNull 和 case when
if(ds.Tables[0].Rows[0]["CONTACTPERSONSEX"].ToString()=="0")
sql IsNull 和 case when   {
sql IsNull 和 case when    ds.Tables
[0].Rows[0]["CONTACTPERSONSEX"]="男";
sql IsNull 和 case when   }
sql IsNull 和 case when   
else
sql IsNull 和 case when   {
sql IsNull 和 case when                ds.Tables
[0].Rows[0]["CONTACTPERSONSEX"]="女";
sql IsNull 和 case when   }
sql IsNull 和 case when
sql IsNull 和 case when 使用CASE WHEN做法。
sql IsNull 和 case when
sql IsNull 和 case when  string sql
="select CUSTOMER.*,CONTACTPERSON.CONTACTPERSONNAME,CONTACTPERSON.CONTACTPERSONBIRTHDAY,CONTACTPERSON.CONTACTPERSONPOSITION,CONTACTPERSON.CONTACTPERSONDEPTNAME,";
sql IsNull 和 case when   sql
+="CONTACTPERSON.TELEPHONE,CONTACTPERSON.MAIL,CONTACTPERSON.CITY,CONTACTPERSON.PROVINCE,CONTACTPERSON.POSTCODE,CONTACTPERSON.COUNTRY,CONTACTPERSON.MEMO,";
sql IsNull 和 case when   sql
+="CONTACTPERSON.DIRECTOR,";
sql IsNull 和 case when    
sql IsNull 和 case when    sql
+="case when CONTACTPERSON.CONTACTPERSONSEX = '0'then '' when CONTACTPERSON.CONTACTPERSONSEX= '1' then '' end as CONTACTPERSONSEX from CUSTOMER ,CONTACTPERSON  where CONTACTPERSONID='"+strContactid+"' and CONTACTPERSON.CUSTOMERID=CUSTOMER.CUSTOMERID";
sql IsNull 和 case when   可以看出这样明显加长了,SQL语句,这样做有什么好处呢?

3、SQL条件控制(case when...then...else...end

sql IsNull 和 case when我的语句:(SQL 
2000
sql IsNull 和 case when
1:
sql IsNull 和 case when
select ByeTime,case when len(ByeTime)>0 then str(DATEDIFF(day,ComeInTime,ByeTime)) else DATEDIFF(day,ComeInTime,getdate()) end as test
sql IsNull 和 case when
from myUser where DelFlag='0' and UserID='cq'
sql IsNull 和 case when
2:
sql IsNull 和 case when
select ByeTime,case when len(ByeTime)>0 then str(DATEDIFF(day,ComeInTime,ByeTime))+'(已离职)' end as test
sql IsNull 和 case when
from myUser where DelFlag='0' and UserID='cq'
sql IsNull 和 case when
3:
sql IsNull 和 case when
select ByeTime,case when len(ByeTime)>0 then str(DATEDIFF(day,ComeInTime,ByeTime))+'(已离职)'
sql IsNull 和 case when
else DATEDIFF(day,ComeInTime,getdate()) end as test
sql IsNull 和 case when
from myUser where DelFlag='0' and UserID='cq'
sql IsNull 和 case when
sql IsNull 和 case when为什么1 和2 执行都没有问题,
3 却报“将 varchar 值 ' 480(已离职)' 转换为数据类型为 int 的列时发生语法错误。”
sql IsNull 和 case when

sql IsNull 和 case when
select ByeTime,case when len(ByeTime)>0 then str(DATEDIFF(day,ComeInTime,ByeTime))+'(已离职)'
sql IsNull 和 case when
else str(DATEDIFF(day,ComeInTime,getdate())) end as test
sql IsNull 和 case when
from myUser where DelFlag='0' and UserID='cq'
sql IsNull 和 case when这样试一下.语法应该没有问题

4、WHEN THEN
sql IsNull 和 case when
WHEN THEN
sql IsNull 和 case when
ELSE
sql IsNull 和 case when
END
sql IsNull 和 case when
--------------
sql IsNull 和 case when
  select count(*as ct , CASE
sql IsNull 和 case when  
WHEN (Pro_state= 1THEN '待审'
sql IsNull 和 case when  
WHEN (Pro_state= 2THEN '已审'
sql IsNull 和 case when  
WHEN (Pro_state= 3THEN '办理中'
sql IsNull 和 case when  
WHEN (Pro_state= 4THEN '已申退'
sql IsNull 和 case when  
WHEN (Pro_state= 5THEN '来信'
sql IsNull 和 case when  
WHEN (Pro_state= 6THEN '办理完成'   
sql IsNull 和 case when  
WHEN (Pro_state= 7THEN '未予立案'   
sql IsNull 和 case when 
end as pro_state
sql IsNull 和 case when
from vw_proposals_query where 1=1 GROUP BY   pro_state
sql IsNull 和 case when 
sql IsNull 和 case when结果: 
sql IsNull 和 case whenct   pro
-state
sql IsNull 和 case when
30   办理中
sql IsNull 和 case when
7    来信
sql IsNull 和 case when
1   办理完成
sql IsNull 和 case when
5   未予立案
sql IsNull 和 case when 
sql IsNull 和 case when解读:
sql IsNull 和 case when查询总数和pro_state 当 Pro_state
= 1 时以 '待审' 替代表示
sql IsNull 和 case when                   当 Pro_state
= 2 时以 '已审' 替代表示
sql IsNull 和 case when                   当 Pro_state
= 3 时以 '办理中' 替代表示
sql IsNull 和 case when                   。。。。。。

5、CASE 可能是 SQL 中被误用最多的关键字之一。虽然你可能以前用过这个关键字来创建字段,但是它还具有更多用法。例如,你可以在 WHERE 子句中使用 CASE
sql IsNull 和 case when
sql IsNull 和 case when首先让我们看一下 
CASE 的语法。在一般的 SELECT 中,其语法如下:
sql IsNull 和 case when
sql IsNull 和 case when
SELECT <myColumnSpec> = 
sql IsNull 和 case when
CASE 
sql IsNull 和 case when
WHEN <A> THEN <somethingA> 
sql IsNull 和 case when
WHEN <B> THEN <somethingB> 
sql IsNull 和 case when
ELSE <somethingE> 
sql IsNull 和 case when
END
sql IsNull 和 case when
sql IsNull 和 case when在上面的代码中需要用具体的参数代替尖括号中的内容。下面是一个简单的例子:
sql IsNull 和 case when
sql IsNull 和 case when
USE pubs 
sql IsNull 和 case when
GO 
sql IsNull 和 case when
SELECT 
sql IsNull 和 case when    Title, 
sql IsNull 和 case when    
'Price Range' = 
sql IsNull 和 case when    
CASE 
sql IsNull 和 case when        
WHEN price IS NULL THEN 'Unpriced' 
sql IsNull 和 case when        
WHEN price < 10 THEN 'Bargain' 
sql IsNull 和 case when        
WHEN price BETWEEN 10 and 20 THEN 'Average' 
sql IsNull 和 case when        
ELSE 'Gift to impress relatives' 
sql IsNull 和 case when    
END 
sql IsNull 和 case when
FROM titles 
sql IsNull 和 case when
ORDER BY price 
sql IsNull 和 case when
GO
sql IsNull 和 case when
sql IsNull 和 case when这是 
CASE 的典型用法,但是使用 CASE 其实可以做更多的事情。比方说下面的 GROUP BY 子句中的 CASE
sql IsNull 和 case when
sql IsNull 和 case when
SELECT 'Number of Titles'Count(*
sql IsNull 和 case when
FROM titles 
sql IsNull 和 case when
GROUP BY 
sql IsNull 和 case when    
CASE 
sql IsNull 和 case when        
WHEN price IS NULL THEN 'Unpriced' 
sql IsNull 和 case when        
WHEN price < 10 THEN 'Bargain' 
sql IsNull 和 case when        
WHEN price BETWEEN 10 and 20 THEN 'Average' 
sql IsNull 和 case when        
ELSE 'Gift to impress relatives' 
sql IsNull 和 case when    
END 
sql IsNull 和 case when
GO
sql IsNull 和 case when
sql IsNull 和 case when你甚至还可以组合这些选项,添加一个 
ORDER BY 子句,如下所示:
sql IsNull 和 case when
sql IsNull 和 case when
USE pubs 
sql IsNull 和 case when
GO 
sql IsNull 和 case when
SELECT 
sql IsNull 和 case when    
CASE 
sql IsNull 和 case when        
WHEN price IS NULL THEN 'Unpriced' 
sql IsNull 和 case when        
WHEN price < 10 THEN 'Bargain' 
sql IsNull 和 case when        
WHEN price BETWEEN 10 and 20 THEN 'Average' 
sql IsNull 和 case when        
ELSE 'Gift to impress relatives' 
sql IsNull 和 case when    
END AS Range, 
sql IsNull 和 case when    Title 
sql IsNull 和 case when
FROM titles 
sql IsNull 和 case when
GROUP BY 
sql IsNull 和 case when    
CASE 
sql IsNull 和 case when        
WHEN price IS NULL THEN 'Unpriced' 
sql IsNull 和 case when        
WHEN price < 10 THEN 'Bargain' 
sql IsNull 和 case when        
WHEN price BETWEEN 10 and 20 THEN 'Average' 
sql IsNull 和 case when        
ELSE 'Gift to impress relatives' 
sql IsNull 和 case when    
END
sql IsNull 和 case when    Title 
sql IsNull 和 case when
ORDER BY 
sql IsNull 和 case when    
CASE 
sql IsNull 和 case when        
WHEN price IS NULL THEN 'Unpriced' 
sql IsNull 和 case when        
WHEN price < 10 THEN 'Bargain' 
sql IsNull 和 case when        
WHEN price BETWEEN 10 and 20 THEN 'Average' 
sql IsNull 和 case when        
ELSE 'Gift to impress relatives' 
sql IsNull 和 case when    
END
sql IsNull 和 case when    Title 
sql IsNull 和 case when
GO
sql IsNull 和 case when
sql IsNull 和 case when注意,为了在 
GROUP BY 块中使用 CASE,查询语句需要在 GROUP BY 块中重复 SELECT 块中的 CASE 块。
sql IsNull 和 case when
sql IsNull 和 case when除了选择自定义字段之外,在很多情况下 
CASE 都非常有用。再深入一步,你还可以得到你以前认为不可能得到的分组排序结果集。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2022-01-29
  • 2021-11-04
  • 2021-06-13
猜你喜欢
  • 2021-07-08
  • 2021-06-14
  • 2022-12-23
  • 2022-12-23
  • 2021-12-30
  • 2021-07-06
  • 2021-05-26
相关资源
相似解决方案