如何在 MSSQL 中将 CST 转换为 UTC

How to convert CST to UTC in MSSQL

是否可以在 MSSQL 中将 CST 转换为 UTC?

例如:

Thu May 13 11:14:19 CST 2021 转换为 2021-05-13 11:14:19.000

正如我在评论中提到的,假设 你的值是一个 datetimeoffset(为什么不是,它是一个带有时间的日期和时间值时区),您可以使用 AT TIME ZONE 更改值的时区。对于单个值,这将是这样的:

DECLARE @YourDate datetimeoffset(0) = '2021-05-13T11:14:19-08:00';
SELECT @YourDate AT TIME ZONE 'UTC';

db<>fiddle

即时转换:

DECLARE @CstTime    varchar(30) = 'Thu May 13 11:14:19 CST 2021'

DECLARE @MonthList  varchar(35) = 'JanFebMarAbrMayJunJulAugSepOctNovDec'
DECLARE @Month      varchar( 2)         
DECLARE @Year       varchar( 4) 
;

SET     @CstTime    =   STUFF(@CstTime,1,4,'')
SET     @Month      =   RIGHT('00' + LTRIM((CHARINDEX(LEFT(@CstTime, 3), @MonthList)+2)/3), 2)
SET     @CstTime    =   REPLACE(@CstTime, ' CST ', '')
SET     @CstTime    =   STUFF(@CstTime,1,4,'-'+@Month+'-')
SET     @Year       =   RIGHT(@CstTime, 4)
SET     @CstTime    =   REPLACE(@CstTime, @Year, '')
SET     @CstTime    =   @Year + '-' + @CstTime

SELECT  @CstTime