SQL DATEDIFF 如果数字为正则添加加号
SQL DATEDIFF add plus sign if number is positive
我正在使用 DATEDIFF( day, date1, date2)
,效果很好。
有没有可能正数有加号,负数有负号
您可以将输出 CAST 或 CONVERT 为字符串,然后根据需要添加“+”号(如果为负数,则减号已经存在。
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
这是一个例子
;With MyTable as
(
Select GETDATE() as Date1, GetDate()+10 as Date2
)
Select
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
From MyTable
;With MyTable as
(
Select GETDATE() as Date1, GetDate()-10 as Date2
)
Select
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
From MyTable
您可以使用
DECLARE @date1 as datetime
DECLARE @date2 as datetime
DECLARE @datediff as varchar(10)
SET @date1 = GETDATE() - 10
SET @date2 = GETDATE() - 1
SET @datediff = DATEDIFF( day, @date1, @date2)
SELECT Case
When @datediff > 0 Then '+'
Else '-'
End + @datediff
您可以使用条件格式:
SELECT FORMAT(DATEDIFF( day, date1, date2),'+#;-#;0')
FROM MyTable
我正在使用 DATEDIFF( day, date1, date2)
,效果很好。
有没有可能正数有加号,负数有负号
您可以将输出 CAST 或 CONVERT 为字符串,然后根据需要添加“+”号(如果为负数,则减号已经存在。
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
这是一个例子
;With MyTable as
(
Select GETDATE() as Date1, GetDate()+10 as Date2
)
Select
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
From MyTable
;With MyTable as
(
Select GETDATE() as Date1, GetDate()-10 as Date2
)
Select
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
From MyTable
您可以使用
DECLARE @date1 as datetime
DECLARE @date2 as datetime
DECLARE @datediff as varchar(10)
SET @date1 = GETDATE() - 10
SET @date2 = GETDATE() - 1
SET @datediff = DATEDIFF( day, @date1, @date2)
SELECT Case
When @datediff > 0 Then '+'
Else '-'
End + @datediff
您可以使用条件格式:
SELECT FORMAT(DATEDIFF( day, date1, date2),'+#;-#;0')
FROM MyTable