在select语句中添加一个单词

时间:2015-10-07 07:11:29

标签: sql sql-server

我想说出" days"在值之后,但是sql server正在做出反应,它无法转换。我怎样才能选择负整数差异。

SELECT PONo, PODeliveryDate,  DATEDIFF(DAY, GETDATE(), PODeliveryDate)  AS DayDiff 
FROM PurchaseOrder 
where POStatus='Complete' OR POStatus='Partially Completed' OR POStatus='Approved'   
ORDER BY ABS( DATEDIFF( DAY, GETDATE(), PODeliveryDate )) 

它给了我日期的不同,但我不能把它放在旁边。

我希望它看起来像5天而不仅仅是5

4 个答案:

答案 0 :(得分:1)

尝试将int值从DATEDIFF()转换为varchar并添加单词

SELECT PONo
,      PODeliveryDate
,      CAST(DATEDIFF(DAY, GETDATE(), PODeliveryDate) as varchar(10)) + ' days'  AS DayDiff 
FROM   PurchaseOrder 
WHERE  POStatus='Complete' 
   OR  POStatus='Partially Completed' 
   OR  POStatus='Approved'   
ORDER BY ABS( DATEDIFF( DAY, GETDATE(), PODeliveryDate ))

答案 1 :(得分:1)

在合并之前将此DATEDIFF(DAY, GETDATE(), PODeliveryDate)转换为VARCHAR

    CONVERT(VARCHAR(50), DATEDIFF(DAY, GETDATE(), PODeliveryDate)) + 'days'

答案 2 :(得分:1)

在sqlserver 2012中,您可以使用CONCAT

要获得正面日期,您可以将getdate作为日期进行转换,并与WHERE语句中的进行比较。然后你不需要由ABS datediff订购。

通过使用IN而不是OR,WHERE子句更易于阅读和维护,它也使维护更容易。

SELECT 
  CONCAT(DATEDIFF(DAY, GETDATE(), PODeliveryDate), ' days')  AS DayDiff 
FROM
  PurchaseOrder 
WHERE
  POStatus in ('Complete','Partially Completed','Approved')
  AND CAST(GETDATE() as date) <= PODeliveryDate
ORDER BY 
  PODeliveryDate

答案 3 :(得分:0)

你能尝试如下:

select CONVERT(varchar(10), GETDATE()) + ' Days'

您需要在连接之前转换或转换为varchar