SQL 服务器 - 在 case 语句中组合日期字段、文字字符和 DATEADD 值
SQL Server - Combining a date field, a literal character and a DATEADD value in a case statment
我已经创建了一个 case 语句来按此顺序工作。
确定它是否是“优先级升级”、“准备取件”、“等待路由”,否则是一个日期范围。
如果我排除 case 语句的 ELSE
部分,则上述方法有效。如果 none 其他情况为真,那么我需要我的 else 语句来生成装运日期加上装运日期加上 2 天,以给出包裹装运的日期范围。即 02/15/2016 – 02/17/2016
Select
Case
When datediff (day, sl.[Shipment Date], sl.[Priority Escalation Date]) < 0 then 'Priority Escalation'
When sh.[On Hold] = 'RFP' then 'Ready for Pickup'
When sh.[On Hold] = 'WFR' then 'Waiting for Routing'
Else sl.[Shipment date] + “ – “ + & DateAdd("d", 2, sl.[Shipment Date])
End as 'Expected Value'
数据应该是这样的
Expected Value
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
Waiting for Routing
Waiting for Routing
Priority Escalation
Ready for Pickup
Ready for Pickup
Priority Escalation
Priority Escalation
Priority Escalation
Ready for Pickup
您可能 运行 遇到转换错误。要解决此问题,您需要 CONVERT
您的日期 VARCHAR
:
SELECT
CASE
WHEN DATEDIFF (DAY, sl.[Shipment Date], sl.[Priority Escalation Date]) < 0 THEN 'Priority Escalation'
WHEN sh.[On Hold] = 'RFP' THEN 'Ready for Pickup'
WHEN sh.[On Hold] = 'WFR' THEN 'Waiting for Routing'
ELSE CONVERT(VARCHAR(10), sl.[Shipment date], 101) + ' - '
+ CONVERT(VARCHAR(10), DATEADD(DAY, 2, sl.[Shipment Date]), 101)
END AS 'Expected Value'
我已经创建了一个 case 语句来按此顺序工作。
确定它是否是“优先级升级”、“准备取件”、“等待路由”,否则是一个日期范围。
如果我排除 case 语句的 ELSE
部分,则上述方法有效。如果 none 其他情况为真,那么我需要我的 else 语句来生成装运日期加上装运日期加上 2 天,以给出包裹装运的日期范围。即 02/15/2016 – 02/17/2016
Select
Case
When datediff (day, sl.[Shipment Date], sl.[Priority Escalation Date]) < 0 then 'Priority Escalation'
When sh.[On Hold] = 'RFP' then 'Ready for Pickup'
When sh.[On Hold] = 'WFR' then 'Waiting for Routing'
Else sl.[Shipment date] + “ – “ + & DateAdd("d", 2, sl.[Shipment Date])
End as 'Expected Value'
数据应该是这样的
Expected Value
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
02/24/2016 - 02/26/2016
Waiting for Routing
Waiting for Routing
Priority Escalation
Ready for Pickup
Ready for Pickup
Priority Escalation
Priority Escalation
Priority Escalation
Ready for Pickup
您可能 运行 遇到转换错误。要解决此问题,您需要 CONVERT
您的日期 VARCHAR
:
SELECT
CASE
WHEN DATEDIFF (DAY, sl.[Shipment Date], sl.[Priority Escalation Date]) < 0 THEN 'Priority Escalation'
WHEN sh.[On Hold] = 'RFP' THEN 'Ready for Pickup'
WHEN sh.[On Hold] = 'WFR' THEN 'Waiting for Routing'
ELSE CONVERT(VARCHAR(10), sl.[Shipment date], 101) + ' - '
+ CONVERT(VARCHAR(10), DATEADD(DAY, 2, sl.[Shipment Date]), 101)
END AS 'Expected Value'