I have a specific example regarding an issue for which there is plenty of general advice on the Internets. However, when it comes to discussions of space and time, I'm no Einstein. So I guess I need a little hand holding from someone who's done this and can vouch for my logic, and also answer a question or two.
My task is simple. I have date/time values in a database that are in UTC. When I run queries where the criteria includes any of these date/time fields I want to apply Central Time. I found a SQL function that does all that. It simply uses the difference between GETUTCDATE and GETDATE (my server is in Central time zone) and uses that difference in seconds to subtract from the given date/time value. Easy enough. However, it does not consider whether the date/time that I am feeding it is in Daylight Savings Time. For example, if I give it the value:
'12/17/2012 9:34:23'
It returns:
'12/17/2012 4:34:23'
which is incorrect. In December the difference between UTC and Central Time is 6 hours, not five. (Correct me if I'm wrong.)
SO...(sorry, had to get that out of the way first)...I decided to write a simple function that does not rely on system time. It simply compares the given date/time value to the national standard for Daylight Savings Time (Currently 2nd Sunday of March at 2 AM to 1st Sunday of November, 2 AM) and adjusts the date accordingly.
So I would do something like:
Get 2nd Sunday of March for the year of the given Date/Time value and call it DSTBegins
Get 1st Sunday of November for the year of the given Date/Time value and call it DSTEnds
If given Date/Time value >= DSTBegins AND Date/Time value < DSTEnds Then
SubtractInt = 5
Else
SubtractInt = 6
End if
Central Time value = Give Date/Time value - SubractInt
(Can you tell I'm more comfortable in VBA than in SQL?)
NOW...here's where you T-SQL gurus come in. How do I turn this logic into a SQL function?
Thanx for reading the whole thing. :0)
tod