使用 case 时将 table 中的时间附加到 getdate()
Append time from table to getdate() while using case
我正在尝试将列中的时间附加到日期并在案例陈述中使用
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'
我也尝试过使用这个解决方案 但仍然出现同样的错误。但是当我这样做时
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
returns 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
returns Incorrect syntax near the keyword 'where'
我不确定是我做错了什么还是限制。
最大的问题是您试图在不使用子查询的情况下使用子查询,这就是语法错误的全部内容。试试这个(为清楚起见展开)
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
我正在尝试将列中的时间附加到日期并在案例陈述中使用
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'
我也尝试过使用这个解决方案 但仍然出现同样的错误。但是当我这样做时
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
returns 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
returns Incorrect syntax near the keyword 'where'
我不确定是我做错了什么还是限制。
最大的问题是您试图在不使用子查询的情况下使用子查询,这就是语法错误的全部内容。试试这个(为清楚起见展开)
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