BACKGROUND:
I have an issue with the day light savings time change. Any records entered into the database (side note: I have no access to the script/code which enters this data to fix it) between 2021-03-28
and 2021-10-31
get entered into the database with the incorrect date. For example:
Records entered in on 2021-03-26
end up in the database as 2021-03-26 00:00:00
, which is correct.
Records entered in on 2021-03-29
end up in the database as 2021-03-28 23:00:00
which is incorrect.
So when I try to search for records entered in on2021-03-26
, the query works fine, but if I try to search for records entered in on 2021-03-29
, it returns records from the wrong date because of the hour change.
SAMPLE DATA:
ColDate, ColName
2021-03-26 00:00:00, SomeName -- CORRECT DATE
2021-03-26 00:00:00, SomeName -- CORRECT DATE
2021-03-26 00:00:00, SomeName -- CORRECT DATE
2021-03-28 23:00:00, SomeName -- INCORRECT DATE
2021-03-28 23:00:00, SomeName -- INCORRECT DATE
2021-03-28 23:00:00, SomeName -- INCORRECT DATE
WORKING EXAMPLE:
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2021-03-26 00:00:00'
SET @EndDate = '2021-03-26 23:59:59'
SELECT *
FROM tblName
WHERE ColDate BETWEEN @StartDate AND @EndDate
The above will return:
ColDate, ColName
2021-03-26 00:00:00, SomeName -- CORRECT DATE
2021-03-26 00:00:00, SomeName -- CORRECT DATE
2021-03-26 00:00:00, SomeName -- CORRECT DATE
NONE WORKING EXAMPLE:
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2021-03-29 00:00:00'
SET @EndDate = '2021-03-29 23:59:59'
SELECT *
FROM tblName
WHERE ColDate BETWEEN @StartDate AND @EndDate
The above will return nothing from the sample data.
QUESTION:
How do I get around this issue? As mentioned earlier, I have no control of the data entry and the developers have no interest in fixing the issue.
Do I need to use IF
statements and check if the date is between 2021-03-28 and 2021-10-31 and adjust the date by 1 hour accordingly? Or is there a better way to resolve this?
UPDATE - POSSIBLE SOLUTION:
The following query seems to work (2021-03-26):
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2021-03-26 00:00:00'
SET @EndDate = '2021-03-26 23:59:59'
SELECT
ColDate AT TIME ZONE 'UTC' AT TIME ZONE 'GMT Standard Time',
ColName
FROM tblName
WHERE ColDate BETWEEN @StartDate AT TIME ZONE 'GMT Standard Time' AT TIME ZONE 'UTC' AND @EndDate AT TIME ZONE 'GMT Standard Time' AT TIME ZONE 'UTC'
The following query seems to work (2021-03-29):
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2021-03-29 00:00:00'
SET @EndDate = '2021-03-29 23:59:59'
SELECT
ColDate AT TIME ZONE 'UTC' AT TIME ZONE 'GMT Standard Time',
ColName
FROM tblName
WHERE ColDate BETWEEN @StartDate AT TIME ZONE 'GMT Standard Time' AT TIME ZONE 'UTC' AND @EndDate AT TIME ZONE 'GMT Standard Time' AT TIME ZONE 'UTC'
UPDATE - QUESTION:
The above update seems to work, but am I overlooking anything?
The grammatically correct usage is “daylight saving time.” The expression is singular and not capitalized, according to the US Government Publishing Office style guide.
By the Energy Policy Act of 2005, daylight saving time (DST) was extended in the United States beginning in 2007. As from that year, DST begins on the second Sunday of March and ends on the first Sunday of November.
From a chronobiological perspective, chronic effects are very likely because, throughout the months of DST, body and social clocks are likely set to different time zones in most people, as we explained above.
United States has observed DST for 105 years between 1918 and 2022 (DST in at least one location). Daylight Saving Time (DST) in the USA starts on the second Sunday in March and ends on the first Sunday in November.
Year-round daylight savings time was used during World War Two and adopted again in 1973 in a bid to reduce energy use because of an oil embargo and repealed a year later.
U.S. Senate approves bill to make daylight saving time permanent | Reuters The U.S. Senate on Tuesday passed legislation that would make daylight saving time permanent starting in 2023, ending the twice-annual changing of clocks in a move promoted by supporters advocating brighter afternoons and more economic activity.
Daylight Saving Time (DST) in most of the United States starts on the second Sunday in March and ends on the first Sunday in November. Which States Don't Use DST? Most of Arizona and Hawaii don't use DST. Indiana introduced DST in 2006. US dependencies do not use Daylight Saving Time (DST). United States first observed Daylight Saving Time in 1918.
Your updated answer seems great. But if the correct entered date is always without time and only incorrect ones are entered with time part then you can also just subtract 1 hour from @startdate while using it in where clause.
DECLARE @StartDate datetime
DECLARE @EndDate datetime
SET @StartDate = '2021-03-29 00:00:00'
SET @EndDate = '2021-03-29 23:59:59'
SELECT *
FROM tblName
WHERE ColDate BETWEEN dateadd(day,-1,@StartDate) AND @EndDate
But it won't work if there are rows with time part other than day light saving issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With