SQLでCASE WHEN使用
一、NULL値判断
1.空の値を判断し、成功しない
2.空の値を判断し、成功
二、If文をCase whenに変更する
if文の使い方を見てみましょう.
上の文をcase when endに変更します.
1.空の値を判断し、成功しない
select case
when null then ' '
else ' '
END as
from
2.空の値を判断し、成功
select case
when IS NULL then ' '
when ='OK' then 'OK '
else ' '
END
from
二、If文をCase whenに変更する
if文の使い方を見てみましょう.
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
end else
if @SelHH=1
begin
select H1 from
end
if @SelHH=2
begin
select H2 from
end
if @SelHH=3
begin
select H3 from
end
if @SelHH=4
begin
select H4 from
end
if @SelHH=5
begin
select H5 from
end
if @SelHH=6
begin
select H6 from
end
if @SelHH=7
begin
select H7 from
end
if @SelHH=8
begin
select H8 from
end
if @SelHH=9
begin
select H9 from
end
if @SelHH=10
begin
select H10 from
end
if @SelHH=11
begin
select H11 from
end
if @SelHH=12
begin
select H12 from
end
if @SelHH=13
begin
select H13 from
end
if @SelHH=14
begin
select H14 from
end
if @SelHH=15
begin
select H15 from
end
if @SelHH=16
begin
select H16 from
end
if @SelHH=17
begin
select H17 from
end
if @SelHH=18
begin
select H18 from
end
if @SelHH=19
begin
select H19 from
end
if @SelHH=20
begin
select H20 from
end
if @SelHH=21
begin
select H21 from
end
if @SelHH=22
begin
select H22 from
end
if @SelHH=23
begin
select H23 from
end
上の文を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