在select语句中获取SQL 2012中的正确时区

时间:2015-02-06 19:04:49

标签: sql sql-server-2008 time timezone

运行以下查询时,我需要获取此SQL查询以在正确的时区输出时间。

SELECT 
     CAST([StartDateTime] AS time(0)) AS 'time' 
     from appointment

当我运行它时,它当前以UTC时间返回所有内容,我需要EST。我已经尝试过switchoffset命令,它可以工作,但我只需要时间来显示,而不是日期和时间。

2 个答案:

答案 0 :(得分:1)

这应该有效:

SELECT CAST(DATEADD(HH,-5,[StartDateTime]) as time) AS 'time'
FROM appointment

答案 1 :(得分:0)

试试这个

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