数据结果应出现在一列中

时间:2015-06-15 08:06:40

标签: sql-server database tsql

我是SQL的自学者;我创建了这段代码:

SELECT T0.[CardName], T0.[DocDate], T0.[DocDueDate], T2.[U_ExpDelDate], T0.[DocStatus], T1.[SlpName], 
CASE
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) <= 0 THEN 'Delivered'
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) >= 0 THEN 'Please Check'
ELSE NULL END AS 'Status',
DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) AS 'Age'
FROM OPOR T0  INNER JOIN OSLP T1 ON T0.[SlpCode] = T1.[SlpCode] INNER JOIN POR1 T2 ON T0.[DocEntry] = T2.[DocEntry]

WHERE T0.[DocStatus] ='O' and T2.[U_ExpDelDate] is not null

我得到了正确的结果,但现在我想在Delivered and Please Check加入结果Age column

你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

使用+运算符或CONCAT方法在sql server中连接字符串。

试试这个:

SELECT T0.[CardName], T0.[DocDate], T0.[DocDueDate], T2.[U_ExpDelDate], T0.[DocStatus], T1.[SlpName], 
CASE
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) <= 0 THEN 'Delivered '
WHEN DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) > 0 THEN 'Please Check ' + CAST(DATEDIFF(day,DocDueDate,T2.[U_ExpDelDate]) as varchar)
END AS 'Age'
FROM OPOR T0  INNER JOIN OSLP T1 ON T0.[SlpCode] = T1.[SlpCode] INNER JOIN POR1 T2 ON T0.[DocEntry] = T2.[DocEntry]

WHERE T0.[DocStatus] ='O' and T2.[U_ExpDelDate] is not null

我已删除else部分,因为它永远不会去那里,datediff函数将返回小于或等于0或大于0的值