简体   繁体   English

如何从AM / PM格式的VARCHAR转换为UTC?

[英]How can I convert to UTC from VARCHAR that's in AM/PM format?

I have a T-SQL VARCHAR column that looks like this: 我有一个看起来像这样的T-SQL VARCHAR列:

APR-10-2018 11:33:39 AM
Dec-04-2017 11:14:24 AM
DEC-01-2017 07:24:54 PM
DEC-01-2017 07:20:28 PM
DEC-01-2017 07:19:52 PM

I want to convert it to UTC. 我想将其转换为UTC。 I realize it will likely require using the CONVERT function. 我意识到可能需要使用CONVERT函数。 However, I have been unsuccessful in my attempts to do this. 但是,我尝试执行此操作一直未成功。 How? 怎么样?

Converting to a DateTime would be small matter. 转换为DateTime将不是一件容易的事。 However, we have no idea what your timezone is, or more importantly, the timezone of the data 但是,我们不知道您的时区是什么,或更重要的是,数据的时区是什么

Example

Declare @YourTable table (SomeCol varchar(50))
Insert Into @YourTable values
 ('APR-10-2018 11:33:39 AM')
,('Dec-04-2017 11:14:24 AM')
,('DEC-01-2017 07:24:54 PM')
,('DEC-01-2017 07:20:28 PM')
,('DEC-01-2017 07:19:52 PM')

Select AsDateTime = try_convert(datetime,replace(SomeCol,'-',' '))
 From  @YourTable

Returns 退货

AsDateTime
2018-04-10 11:33:39.000
2017-12-04 11:14:24.000
2017-12-01 19:24:54.000
2017-12-01 19:20:28.000
2017-12-01 19:19:52.000

You need to read up on cast() and convert() : https://docs.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017 您需要阅读cast()convert()https : //docs.microsoft.com/zh-cn/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-服务器2017

[Hint: you have to specify the desired date/time style). [提示:您必须指定所需的日期/时间样式)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM