2015-01-14 1 views

ответ

2

Вы можете 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 
1

Вы можете использовать

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 
2

Вы можете использовать условное форматирование:

SELECT FORMAT(DATEDIFF(day, date1, date2),'+#;-#;0') 
FROM MyTable