Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use of CASE WHEN in SQL

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

1. NULL value judgment 1. Judge null value, unsuccessful select case field name when null then 'null' else 'non-null' END as Field Alias from Table 2. Determine null value, select case successfully when field name IS NULL then 'null' when field name ='OK' then 'OK' else 'non-null' END field alias from Table 2: Change the If statement to Case when

Let's look at how to write an if statement:

declare @SelDD smalldatetime,@SelHH tinyint set @SelDD=cast(convert(varchar(19),Getdate(),111) as smalldatetime) set @SelHH=datepart(hour,Getdate()) if @SelHH=0 begin select H0 from table end else if @SelHH=1 begin select H1 from Table end if @SelHH=2 begin select H2 from Table end if @SelHH=3 begin select H3 from Table end if @SelHH=4 begin select H4 from Table end if @SelHH=5 begin select H5 from Table end if @SelHH=6 begin select H6 from Table end if @SelHH=7 begin select H7 from Table end if @SelHH=8 begin select H8 from Table end if @SelHH=9 begin select H9 from Table end if @SelHH=10 begin select H10 from Table end if @SelHH=11 begin select H11 from Table end if @SelHH=12 begin select H12 from Table end if @SelHH=13 begin select H13 from Table end if @SelHH=14 begin select H14 from Table end if @SelHH=15 begin select H15 from Table end if @SelHH=16 begin select H16 from Table end if @SelHH=17 begin select H17 from Table end if @SelHH=18 begin select H18 from Table end if @SelHH=19 begin select H19 from Table end if @SelHH=20 begin select H20 from Table end if @SelHH=21 begin select H21 from Table end if @SelHH=22 begin select H22 from Table end if @SelHH=23 begin select H23 from Table end

Change the above statement to case when end:

select case @SelHH when 0 then H0 when 1 then H1 when 2 then H2 when 3 then H3 when 4 then H4 when 5 then H5 when 6 then H6 when 7 then H7 when 8 then H8 when 9 then H9 when 10 then H10 when 11 then H11 when 12 then H12 when 13 then H13 when 14 then H14 when 15 then H15 when 16 then H16 when 17 then H17 when 18 then H18 when 19 then H19 when 20 then H20 when 21 then H21 when 22 then H22 when 23 then H23 end from Data Sheet

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report