在 select 语句中获取 SQL 2012 中的正确时区
Get correct time zone in SQL 2012 on select statement
我需要获取此 SQL 查询以在 运行 执行以下查询时输出正确时区的时间。
SELECT
CAST([StartDateTime] AS time(0)) AS 'time'
from appointment
当我 运行 这样做时,它目前 return 一切都在 UTC 时间,我需要 EST。我试过 switchoffset 命令,它可以工作,但我只需要显示时间而不是日期和时间。
这应该有效:
SELECT CAST(DATEADD(HH,-5,[StartDateTime]) as time) AS 'time'
FROM appointment
试试这个
DECLARE @appointment TABLE
(
id integer,
StartDateTime datetime
)
INSERT INTO @appointment SELECT 1, '1/1/2014 13:30'
INSERT INTO @appointment SELECT 2, '1/4/2014 4:00'
INSERT INTO @appointment SELECT 3, '1/6/2014 15:30'
INSERT INTO @appointment SELECT 4, '1/8/2014 23:00'
SELECT
CAST(SWITCHOFFSET(CONVERT(DATETIMEOFFSET, StartDateTime), '+01:00') as TIME(0)) as StartDateTime
FROM @appointment
我需要获取此 SQL 查询以在 运行 执行以下查询时输出正确时区的时间。
SELECT
CAST([StartDateTime] AS time(0)) AS 'time'
from appointment
当我 运行 这样做时,它目前 return 一切都在 UTC 时间,我需要 EST。我试过 switchoffset 命令,它可以工作,但我只需要显示时间而不是日期和时间。
这应该有效:
SELECT CAST(DATEADD(HH,-5,[StartDateTime]) as time) AS 'time'
FROM appointment
试试这个
DECLARE @appointment TABLE
(
id integer,
StartDateTime datetime
)
INSERT INTO @appointment SELECT 1, '1/1/2014 13:30'
INSERT INTO @appointment SELECT 2, '1/4/2014 4:00'
INSERT INTO @appointment SELECT 3, '1/6/2014 15:30'
INSERT INTO @appointment SELECT 4, '1/8/2014 23:00'
SELECT
CAST(SWITCHOFFSET(CONVERT(DATETIMEOFFSET, StartDateTime), '+01:00') as TIME(0)) as StartDateTime
FROM @appointment