あなたの日付は、日付のように定義されるように表示されない、そして、あなたがこれを知っていれば私を許しますが、空の文字列がnullと同じではありません。
以下の例は、あなたがしたいと思うものを示していますが、適切なデータ型を使用しています。適切なデータ型を使用することを強くお勧めします。
そして、誤解を取り除くために、case文の1つの条件が真であるとすぐに、続くwhen節は評価されません。 err1、err2、およびerr3は独立したcase文であるため、常にすべて評価する必要がありますが、ネストされたcase文はerr1に依存します。
また、char(13)はキャリッジリターンです。これにより出力行が上書きされます。おそらく、あなたは改行(またはあなたがWindows上で動いているならば両方)であるchar(10)を望んでいます。
create table so48010818
(
id int,
approval_date date,
first_spend date,
funding_status varchar(1),
funding_type varchar(1)
);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (1, null, '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (2, '2017-12-20', '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (3, '2017-12-12', '2017-12-28', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (4, '2017-12-27', '2017-12-01', '', '');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (5, null, '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (6, '2017-12-20', '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (7, '2017-12-12', '2017-12-28', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (8, '2017-12-27', '2017-12-01', null, null);
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (5, null, '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (6, '2017-12-20', '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (7, '2017-12-12', '2017-12-28', 'X', 'Y');
insert into so48010818 (id, approval_date, first_spend, funding_status, funding_type) values (8, '2017-12-27', '2017-12-01', 'X', 'Y');
select * from so48010818;
select
id,
approval_date,
first_spend,
funding_status,
funding_type,
case
when approval_date is null then 'approval date is missing (null)'
else -- approval_date cannot be null here
case
when first_spend > dateadd(day, 15, approval_date) then 'approval date is too far in the past'
when dateadd(day, 25, first_spend) < approval_date then 'approval date is too far in the future'
end
end as 'err1',
case
when funding_status is null then 'funding status is missing (null)'
when funding_status = '' then 'funding status is missing (empty string)'
end as 'err2',
case
when funding_type is null then 'funding type is missing (null)'
when funding_type = '' then 'funding type is missing (empty string)'
end as 'err3'
from
so48010818;
id approval_date first_spend funding_status funding_type err1 err2 err3
----------- ---------------- ---------------- -------------- ------------ ------------------------------------- ---------------------------------------- ----------------------------- --------
1 NULL 2017-12-28 approval date is missing (null) funding status is missing (empty string) funding type is missing (empty string)
2 2017-12-20 2017-12-28 NULL funding status is missing (empty string) funding type is missing (empty string)
3 2017-12-12 2017-12-28 approval date is too far in the past funding status is missing (empty string) funding type is missing (empty string)
4 2017-12-27 2017-12-01 approval date is too far in the future funding status is missing (empty string) funding type is missing (empty string)
5 NULL 2017-12-28 NULL NULL approval date is missing (null) funding status is missing (null) funding type is missing (null)
6 2017-12-20 2017-12-28 NULL NULL NULL funding status is missing (null) funding type is missing (null)
7 2017-12-12 2017-12-28 NULL NULL approval date is too far in the past funding status is missing (null) funding type is missing (null)
8 2017-12-27 2017-12-01 NULL NULL approval date is too far in the future funding status is missing (null) funding type is missing (null)
5 NULL 2017-12-28 X Y approval date is missing (null) NULL NULL
6 2017-12-20 2017-12-28 X Y NULL NULL NULL
7 2017-12-12 2017-12-28 X Y approval date is too far in the past NULL NULL
8 2017-12-27 2017-12-01 X Y approval date is too far in the future NULL NULL
(12 rows affected)
まあ、approval_dateは常に「NULL」または「0」でなければなりません。 –
クエリの期待値と実際の出力に関するサンプルデータはありますか? –
ネストされたケースに欠けているelsesを追加しようとしましたか? – fabricio