I am using DATEDIFF( day, date1, date2)
and it works great.
我使用DATEDIFF(day, date1, date2),它非常好用。
Is it possible to have the plus sign if a number is positive and if it is negative, it has a minus.
如果一个数是正的,如果它是负的,它可能有加号,它有一个负号。
3 个解决方案
#1
2
You can CAST or CONVERT the output to a string, and then add the '+' sign if required (the minus will already be there if it is negative.
您可以将输出转换为字符串,然后在需要时添加'+'符号(如果为负,则负号已经存在)。
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
Here is an example
这是一个例子
;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
#2
2
You can use conditional formatting:
可以使用条件格式:
SELECT FORMAT(DATEDIFF( day, date1, date2),'+#;-#;0')
FROM MyTable
#3
1
You can use
您可以使用
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
#1
2
You can CAST or CONVERT the output to a string, and then add the '+' sign if required (the minus will already be there if it is negative.
您可以将输出转换为字符串,然后在需要时添加'+'符号(如果为负,则负号已经存在)。
Case When DATEDIFF( day, date1, date2) > 0 Then '+' Else '' End + Cast (DATEDIFF( day, date1, date2) as VarChar(10))
Here is an example
这是一个例子
;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
#2
2
You can use conditional formatting:
可以使用条件格式:
SELECT FORMAT(DATEDIFF( day, date1, date2),'+#;-#;0')
FROM MyTable
#3
1
You can use
您可以使用
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