【问题标题】:Append time from table to getdate() while using case在使用案例时将时间从表附加到 getdate()
【发布时间】:2020-01-07 17:27:00
【问题描述】:

我正在尝试将时间从列附加到日期并在案例陈述中使用

getdate() at time zone 'Central Standard Time'

这是我迄今为止尝试过的

select case when 
getdate() at time zone 'Central Standard Time'  <= 
cast(cast (getdate() at time zone 'Central Standard Time'as date) as datetime) + endtime from tablename where id = 'uniqueid'
1 else 0 end

endtime列的数据类型为nvarchar,时间格式为21:00

这是我得到的错误

Incorrect syntax near the keyword 'from'

我也尝试过使用此解决方案https://stackoverflow.com/a/700647,但仍然出现相同的错误。但是当我这样做时

select CAST(CAST(getdate() at time zone 'Central Standard Time' AS DATE) AS DATETIME) + endtime from tablename where id = 'uniqueid'

我得到了2020-01-07 07:00:00.000的正确回复

这似乎也可以正常工作

select case when
getdate() at time zone 'Central Standard Time' >= 
CAST(CAST(CAST(getdate() at time zone 'Central Standard Time'AS DATE) AS DATETIME) + '' + '10:00'as DATETIME)
then 1 else 0
end

返回 1

但是

select case when 
getdate() at time zone 'Central Standard Time' <=
cast(CAST(CAST(getdate() at time zone 'Central Standard Time' AS DATE) AS DATETIME) + endtime from table where id = 'uniqueid' as datetime) 
then 1 else 0 end

返回Incorrect syntax near the keyword 'where'

我不确定我做错了什么还是限制。

【问题讨论】:

    标签: sql sql-server sql-server-2017


    【解决方案1】:

    最大的问题是您尝试使用子查询而不使用子查询,这就是语法错误的全部原因。试试这个(为了清楚起见,展开)

    SELECT 
        CASE 
            WHEN getdate() at TIME zone 'Central Standard Time' <= 
                (SELECT cast(cast(getdate() at TIME zone 'Central Standard Time' AS DATE) AS DATETIME) + endtime FROM tablename WHERE id = 'uniqueid') -- Subquery
                THEN 1 
            ELSE 0 
        END
    

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 1970-01-01
      • 2021-12-13
      • 2012-09-19
      • 2023-01-13
      • 1970-01-01
      • 2021-12-27
      • 2016-06-25
      • 1970-01-01
      相关资源
      最近更新 更多