我在 SQL Server 中有以下代碼:
[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_dateANDps.stop_required_delivery_time都不為 null 時才回傳 concat。
我嘗試添加:
and ps.stop_required_delivery_time is not null
但這只是回傳了 else 陳述句,它是 'NULL' !
如果兩列都不為空,我該如何撰寫代碼以僅回傳連接文本?如果一個為空而另一個填充回傳
then concat(pt.[TOUR_PLANNED_START_DATE], 'T', dateadd(minute, 30, pt.[TOUR_PLANNED_START_TIME]))
提前謝謝了
uj5u.com熱心網友回復:
IS NULL并在以下示例中IS NOT NULL完美地作業。
我使用資料型別日期和時間使其盡可能接近您的情況,而無需您的表定義和示例資料。CASE
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); GO4 行受影響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一個 | 乙 | 抗體 :--------- | :--------------- | :---------------------------- 2022-01-01 | 10:00:00.0000000 | 2022-01-01T10:00:00.0000000 2022-01-01 | 空 | 一個空 null | 10:00:00.0000000 | 空-b 空 | 空 | 空-空
db<>在這里擺弄
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/466142.html
標籤:sql服务器
上一篇:如何從SQL資料庫查詢中過濾掉值
