2017-09-14 18 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 - 所以存在类型冲突。