如果两列都不是 NULL,我如何获得 SQL 'When' 子句以仅 return concat 数据?
how do I get a SQL 'When' clause to only return concat data if both columns are NOT NULL?
我在 SQL 服务器中有以下代码:
[siteWindowStart] = case
when loc.LOCATION_TYPE_ID = '1'
and ps.STOP_PLANNED_START_DATE is null
then concat(pt.[TOUR_PLANNED_START_DATE], 'T', '09:00:00')
when loc.LOCATION_TYPE_ID = '1'
and ps.STOP_PLANNED_START_DATE is not null
then concat(ps.STOP_PLANNED_START_DATE, 'T', '09:00:00')
when loc.LOCATION_TYPE_ID in ('6', '7', '8')
and ps.STOP_REQUIRED_DELIVERY_DATE is not null
then concat(dateadd(day, -1, ps.STOP_REQUIRED_DELIVERY_DATE), 'T' ,ps.[STOP_REQUIRED_DELIVERY_TIME])
when loc.LOCATION_TYPE_ID in ('6', '7', '8')
and ps.STOP_REQUIRED_DELIVERY_DATE is null
then CONCAT(dateadd(day, -1, pt.[TOUR_PLANNED_START_DATE]), 'T', pt.[TOUR_PLANNED_START_TIME])
else null
end
对于这部分代码...
and ps.STOP_REQUIRED_DELIVERY_DATE is not null
then concat(ps.STOP_REQUIRED_DELIVERY_DATE, 'T', dateadd(minute, 30, ps.[STOP_REQUIRED_DELIVERY_TIME]))
如果 ps.stop_required_delivery_date
和 ps.stop_required_delivery_time
都不为空,我需要确保它仅 return 是 concat。
我尝试添加:
and ps.stop_required_delivery_time is not null
但这只是 returned else 语句,即 'NULL' !
如果两列都不为空,我如何将其编码为仅return concat 文本?如果一个为空而另一个已填充 return
then concat(pt.[TOUR_PLANNED_START_DATE], 'T', dateadd(minute, 30, pt.[TOUR_PLANNED_START_TIME]))
非常感谢
IS NULL
和 IS NOT NULL
在 CASE
中完美工作,如下例所示。
我已经使用数据类型日期和时间使其尽可能接近您的情况,而无需您的 table 定义和示例数据。
CREATE TABLE t ( a date, b time);
insert into t values ('2022-01-01','10:00'),('2022-01-01',null),(null,'10:00'),(null,null);
GO
4 rows affected
select
a,
b,
case
when a is not null and b is not null then concat(a,'T',b)
when a is not null and b is null then 'a-null'
when a is null and b is not null then 'null-b'
when a is null and b is null then 'null-null'
else 'not possible'
end "a-b"
from t
GO
a | b | a-b
:--------- | :--------------- | :--------------------------
2022-01-01 | 10:00:00.0000000 | 2022-01-01T10:00:00.0000000
2022-01-01 | null | a-null
null | 10:00:00.0000000 | null-b
null | null | null-null
db<>fiddle here
我在 SQL 服务器中有以下代码:
[siteWindowStart] = case
when loc.LOCATION_TYPE_ID = '1'
and ps.STOP_PLANNED_START_DATE is null
then concat(pt.[TOUR_PLANNED_START_DATE], 'T', '09:00:00')
when loc.LOCATION_TYPE_ID = '1'
and ps.STOP_PLANNED_START_DATE is not null
then concat(ps.STOP_PLANNED_START_DATE, 'T', '09:00:00')
when loc.LOCATION_TYPE_ID in ('6', '7', '8')
and ps.STOP_REQUIRED_DELIVERY_DATE is not null
then concat(dateadd(day, -1, ps.STOP_REQUIRED_DELIVERY_DATE), 'T' ,ps.[STOP_REQUIRED_DELIVERY_TIME])
when loc.LOCATION_TYPE_ID in ('6', '7', '8')
and ps.STOP_REQUIRED_DELIVERY_DATE is null
then CONCAT(dateadd(day, -1, pt.[TOUR_PLANNED_START_DATE]), 'T', pt.[TOUR_PLANNED_START_TIME])
else null
end
对于这部分代码...
and ps.STOP_REQUIRED_DELIVERY_DATE is not null
then concat(ps.STOP_REQUIRED_DELIVERY_DATE, 'T', dateadd(minute, 30, ps.[STOP_REQUIRED_DELIVERY_TIME]))
如果 ps.stop_required_delivery_date
和 ps.stop_required_delivery_time
都不为空,我需要确保它仅 return 是 concat。
我尝试添加:
and ps.stop_required_delivery_time is not null
但这只是 returned else 语句,即 'NULL' !
如果两列都不为空,我如何将其编码为仅return concat 文本?如果一个为空而另一个已填充 return
then concat(pt.[TOUR_PLANNED_START_DATE], 'T', dateadd(minute, 30, pt.[TOUR_PLANNED_START_TIME]))
非常感谢
IS NULL
和 IS NOT NULL
在 CASE
中完美工作,如下例所示。
我已经使用数据类型日期和时间使其尽可能接近您的情况,而无需您的 table 定义和示例数据。
CREATE TABLE t ( a date, b time); insert into t values ('2022-01-01','10:00'),('2022-01-01',null),(null,'10:00'),(null,null); GO
4 rows affectedselect a, b, case when a is not null and b is not null then concat(a,'T',b) when a is not null and b is null then 'a-null' when a is null and b is not null then 'null-b' when a is null and b is null then 'null-null' else 'not possible' end "a-b" from t GO
a | b | a-b :--------- | :--------------- | :-------------------------- 2022-01-01 | 10:00:00.0000000 | 2022-01-01T10:00:00.0000000 2022-01-01 | null | a-null null | 10:00:00.0000000 | null-b null | null | null-null
db<>fiddle here