我的sql脚本出了什么问题?它总是说不正确的语法。
Select Case
When(DAY(GETDATE()) <= 23 and DAY(GETDATE()) >= 9) Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'9'
When(DAY(GETDATE()) <= 8 and DAY(GETDATE()) >= 24) Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'24'
根据当前日期显示2012-11-9或2012-11-24。
答案 0 :(得分:4)
可能你错过了end
,试试这个:
Select Case
When(DAY(GETDATE()) <= 23 and DAY(GETDATE()) >= 9)
Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'9'
When(DAY(GETDATE()) <= 8 and DAY(GETDATE()) >= 24)
Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'24'
end as Date1
from YourTable
答案 1 :(得分:3)
您需要End
您的案例
Select Case
When(DAY(GETDATE()) <= 23 and DAY(GETDATE()) >= 9) Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'9'
When(DAY(GETDATE()) <= 8 and DAY(GETDATE()) >= 24) Then Convert(varchar(10),YEAR(getdate()))+'-'+Convert(varchar(10),MONTH(getdate()))+'-'+'24'
End
答案 2 :(得分:3)
除了遗失的end
之外,您的case
条件之一永远不会成为现实
When(DAY(GETDATE()) <= 8 and DAY(GETDATE()) >= 24)
使用
case when DAY(GETDATE()) between 9 and 23 then CONVERT(varchar(8),getdate(),120) + '9'
else CONVERT(varchar(8),getdate(),120) + '24'
end
此外,您的逻辑看起来可能是可疑的。你真的想在十月二十四日回到10月2日和30日吗?