Adjust utc date & time to cst date & time

How can i get my results to show CST instead of the time stamped UTC in SignalHistory.AlarmDate?
SELECT SignalHistory.AlarmDate, IsNull(Subscriber.AcctLineCode, 'XX'), Subscriber.AcctNum, Subscriber.Name, Codes.Description, SignalHistory.AlarmZones, IsNull(Zones.Description, ''), Subscriber.Address, Subscriber.City, Subscriber.State, SignalHistory.RawData
FROM [History0218].[dbo].[SignalHistory] SignalHistory
   LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
   LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
   LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -1, GETUTCDATE())AND (Subscriber.AcctLineCode ='DS'OR Subscriber.AcctLineCode ='DSA')
ORDER BY AlarmDate DESC

Open in new window

LVL 1
csePixelatedAsked:
Who is Participating?
 
pcelbaCommented:
To display CST is relatively easy because it is calculated as UTC - 6:
SELECT SignalHistory.AlarmDate, DATEADD(HOUR, -6, SignalHistory.AlarmDate) AlarmDateCST, 
       IsNull(Subscriber.AcctLineCode, 'XX'), Subscriber.AcctNum, Subscriber.Name, Codes.Description, SignalHistory.AlarmZones, IsNull(Zones.Description, ''), Subscriber.Address, Subscriber.City, Subscriber.State, SignalHistory.RawData
FROM [History0218].[dbo].[SignalHistory] SignalHistory
   LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
   LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
   LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -1, GETUTCDATE())AND (Subscriber.AcctLineCode ='DS'OR Subscriber.AcctLineCode ='DSA')
ORDER BY AlarmDate DESC

Open in new window

More difficult or more complex would be to display the time in CDT during the daylight saving period when needed. Is CDT also required?

Update: If you are using SQL 2016 then you may look at DateTimeOffset data type and use AT TIME ZONE to display the time with appropriate daylight saving offset. Of course, you have to know the location (time zone) where the time is used. More info: https://docs.microsoft.com/en-us/sql/t-sql/queries/at-time-zone-transact-sql
0
 
ste5anSenior DeveloperCommented:
DATETIMEOFFSET is available since SQL Server 2008. AT TIME ZONE is new.
0
 
csePixelatedAuthor Commented:
Pcebla, did not need the extra column in the output, this worked, thank you
SELECT DATEADD(HOUR, -6, SignalHistory.AlarmDate) 'Alarm Date', 
       IsNull(Subscriber.AcctLineCode, 'XX') AS LC, Subscriber.AcctNum, Subscriber.Name, Codes.Description, SignalHistory.AlarmZones as 'zone #', IsNull(Zones.Description, '') AS 'Zone Description', Subscriber.Address, Subscriber.City, Subscriber.State, SignalHistory.RawData
FROM [History0218].[dbo].[SignalHistory] SignalHistory
   LEFT JOIN [SUBSCRIBER].[dbo].[Subscriber Data] Subscriber ON SignalHistory.AccountID = Subscriber.AccountID
   LEFT JOIN [SUBSCRIBER].[dbo].[Zone Lists] Zones ON SignalHistory.AccountID = Zones.AccountID AND LTRIM(SignalHistory.AlarmZones) = LTRIM (Zones.Number)
   LEFT JOIN [MISCELLANEOUS].[dbo].[Signal Codes] Codes ON SignalHistory.AlarmCode = Codes.Code
WHERE SignalHistory.AlarmDate >= DATEADD(HOUR, -1, GETUTCDATE())AND (Subscriber.AcctLineCode ='DS'OR Subscriber.AcctLineCode ='DSA')
ORDER BY AlarmDate DESC

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.