我想运行一个简单的T-SQL SELECT查询,这样输入(datetimeoffset)......
2015-01-01 00:15:00.0000000 +01:00
OR
2015-05-04 14:15:00.0000000 +02:00
...作为此输出(datetime)出现:
2015-01-01 01:15:00
OR
2015-05-04 16:15:00
输入是一列,输出也应该是一列。
有什么想法吗?
更新20170126
一如既往,它永远不会像我想象的那么容易。该查询是更大查询的一部分,我现在已经制定如下(见下文),输入列被命名为' TimeStamp'并且输出列被命名为' Tijd'。这并不起作用,因为它似乎没有在变量声明中识别[TimeStamp]。我错过了什么?
DECLARE @dt datetimeoffset = (SELECT CONVERT(datetimeoffset, [TimeStamp]))
SELECT @dt as Original
,CONVERT(datetime2,@dt,1) AS Tijd
,[Id]
,[EanCode]
,[DataAccessPointId]
,[DataSource]
,[ElectricityUsageNormalkWh] AS Piek
,[ElectricityUsageLowkWh] AS Dal
,[DateAltKey] = CONVERT(int, CONVERT(varchar(8), [TimeStamp], 112))
,[TimeAltKey] = DATEPART(hh,[TimeStamp]) * 10000 + DATEPART(mi,[TimeStamp]) * 100 + DATEPART(ss,[TimeStamp])
FROM [dbo].[ElectricityTelemetryData]
答案 0 :(得分:3)
我认为在关于+/-值的问题中,你的时间偏移逻辑是错误的,但根据你想要输出的数据类型,你有几个选项:
declare @dt datetimeoffset = (select convert(datetimeoffset,'2015-01-01 00:15:00.0000000 +01:00'))
select @dt as Original
,convert(datetime2,@dt,1) as Converted
,switchoffset(@dt,'+00:00') as Switched
输出:
Original | Converted | Switched
2015-01-01 00:15:00.0000000 +01:00 | 2014-12-31 23:15:00.0000000 | 2014-12-31 23:15:00.0000000 +00:00