这很混乱,因为您的数据是。您还有很长的路要走,我建议实际上将您的“ DateTime
”列更改为正确的数据类型,而不是使用计算列:
--Create sample table
CREATE TABLE dbo.YourTable (CountryCode char(2),
NotADateTime varchar(50));
INSERT INTO dbo.YourTable (CountryCode,
NotADateTime)
VALUES ('EE', 'Sun Mar 29 00:00:00 GMT 1992'),
('AZ', 'Sun Mar 28 00:00:00 GMT 1993'),
('CZ', 'Sun Mar 27 00:00:00 GMT 1994'),
('DE', 'Sun Mar 26 00:00:00 GMT 1995'),
('AD', 'Sun Mar 31 00:00:00 GMT 1996'),
('GI', 'Sun Mar 30 00:00:00 GMT 1997'),
('DK', 'Sun Mar 29 00:00:00 GMT 1998'),
('AL', 'Sun Mar 28 00:00:00 GMT 1999'),
('ES', 'Sun Mar 26 00:00:00 GMT 2000'),
('LT', 'Sun Mar 25 00:00:00 GMT 2001'),
('FR', 'Sun Jul 14 00:00:00 GMT 2019'); --Add one that is in DST.
GO
--Create a proper datetimeoffset column
ALTER TABLE dbo.YourTable ADD ADateTime AS TRY_CONVERT(datetimeoffset(0),RIGHT(NotADateTime,4) + '-' +
CASE SUBSTRING(NotADateTime,5,3) WHEN 'Jan' THEN '01'
WHEN 'Feb' THEN '02'
WHEN 'Mar' THEN '03'
WHEN 'Apr' THEN '04'
WHEN 'May' THEN '05'
WHEN 'Jun' THEN '06'
WHEN 'Jul' THEN '07'
WHEN 'Aug' THEN '08'
WHEN 'Sep' THEN '09'
WHEN 'Oct' THEN '10'
WHEN 'Nov' THEN '11'
WHEN 'Dec' THEN '12'
END + '-' + SUBSTRING(NotADateTime,9,2) + 'T' +
SUBSTRING(NotADateTime,12,9));
GO
--Check data
SELECT YT.CountryCode,
YT.NotADateTime,
YT.ADateTime
FROM dbo.YourTable YT;
GO
--Add a timezone lookup table
CREATE TABLE dbo.TimeZoneLookup (CountryCode char(2),
MSTimeZoneName nvarchar(128));
INSERT INTO dbo.TimeZoneLookup (CountryCode,
MSTimeZoneName)
SELECT CountryCode,'Central Europe Standard Time' --All apepar to be european countries, so jsut assumed. You should NOT.
FROM dbo.YourTable; --You can find all the timezones in sys.time_zone_info
GO
--JOIN and change timezone
SELECT YT.CountryCode,
YT.ADateTime,
YT.ADateTime AT TIME ZONE TZ.MSTimeZoneName
FROM dbo.YourTable YT
JOIN dbo.TimeZoneLookup TZ ON YT.CountryCode = TZ.CountryCode
GO
--Clean up
DROP TABLE dbo.YourTable;
DROP TABLE dbo.TimeZoneLookup;