날짜가 날짜로 정의되지 않는 것 같습니다, 그리고, 당신이 알고있는 경우에 저를 용서하지만, 빈 문자열이 널 (null)과 동일하지 않습니다 .
아래 예제는 내가하고 싶은 생각을 보여 주지만 적절한 데이터 유형을 사용합니다. 적절한 데이터 유형을 사용하는 것이 좋습니다.
오해를 제거하기 위해 case 문에서 하나의 조건이 참하자마자 다음 "when"절이 평가되지 않습니다. err1, err2 및 err3은 독립적 인 case 문이므로 항상 모두 평가되어야하지만 중첩 된 case 문은 err1에 종속됩니다.
또한 char (13)은 캐리지 리턴입니다. 그러면 출력 라인을 덮어 씁니다. 아마도 당신은 개행 문자 인 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