简体   繁体   中英

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:

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. I realize it will likely require using the CONVERT function. However, I have been unsuccessful in my attempts to do this. How?

Converting to a DateTime would be small matter. 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

[Hint: you have to specify the desired date/time style).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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