2017-09-14 21 views
1

我使用下面的查詢,但得到的錯誤設置CASE語句,以便所有的結果類型爲VARCHAR

'Conversion failed when converting the varchar value '8-14' to data type int.' 

我相信它,因爲第二行是返回一個整數,其餘的不是,是正確的?

我不打算計算這個字段,所以如果他們都需要是相同類型的值,讓第二行返回爲varchar會很好。我嘗試過使用'convert',但是我不想我認爲我的語法沒錯。

,(case when AppointmentCancellationDate = null Then 'NoDate' 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) < '8' then (datediff(day,BasicStartDate,AppointmentCancellationDate)) 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) <15 then '8-14' 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) <22 then '15-21' 
Else '+22' end) as CancWindowGroup 

回答

3

嘗試以下:

,(case when AppointmentCancellationDate is null Then 'NoDate' 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) <8 then cast(datediff(day,BasicStartDate,AppointmentCancellationDate) as nvarchar(10)) 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) <15 then '8-14' 
when (datediff(day,BasicStartDate,AppointmentCancellationDate)) <22 then '15-21' 
Else '+22' end) as CancWindowGroup 
+0

感謝看起來像它的工作 –

+1

並從'<'8''更改爲'<8' –

+1

並將'= null'更改爲'爲空' – TriV

1

與 '野點' 定義的情況下爲varchar的結果。第二行產生int - 所以存在類型衝突。