0

我的 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。

4

3 回答 3

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
于 2012-11-22T09:29:00.173 回答
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
于 2012-11-22T09:29:18.647 回答
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 日都返回 10 月 24 日吗?

于 2012-11-22T09:35:42.760 回答