Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:14:25+00:00 2026-05-15T20:14:25+00:00

Right now I’m storing a number of records in SQL Server with a DATETIME

  • 0

Right now I’m storing a number of records in SQL Server with a DATETIME column that stores the current timestamp using GETUTCDATE(). This ensures that we’re always storing the exact date without having to worry about questions like “well is this 2:00 my time or 2:00 your time?” Using UTC ensures that we know exactly when it happened regardless of timezone.

However, I have a query that essentially groups these records by date. A simplified version of this query looks something like this:

SELECT [created], SUM([amount]) AS [amount]
FROM (
    SELECT [amount], LEFT(CONVERT(VARCHAR, [created], 120), 10) AS [created]
    FROM (
        SELECT [amount], DATEADD(HOUR, -5, [created]) AS [created]
        FROM [sales]
        WHERE [organization] = 1
    ) AS s
) AS s
GROUP BY [created]
ORDER BY [created] ASC

Obviously this query is far from ideal–the whole reason I’m here is to ask how to improve it. First of all, it does (for the most part) accomplish the goal of what I’m looking for here–it has things grouped by dates and the other values aggregated accordingly. But what it doesn’t accomplish is handling Daylight Savings Time correctly.

I live in Madison, WI and we’re on Central Time time, so between March and November we’re UTC-5, otherwise we’re UTC-6. That’s why you see the -5 in the code there as a quick hack to get it working.

The problem is that if I run this query, and there are records that fall on both sides of the daylight savings time changeover, it could potentially group things incorrectly. So for instance, if the table looks something like this:

+----+--------+---------------------+
| id | amount | created             |
+----+--------+---------------------+
|  1 | 100.00 | 2010-04-02 06:00:00 |
|  2 |  50.00 | 2010-04-02 04:30:00 |
|  3 |  75.00 | 2010-04-02 03:00:00 |
|  4 | 150.00 | 2010-03-02 07:00:00 |
|  5 |  25.00 | 2010-03-02 05:30:00 |
|  6 |  50.00 | 2010-03-02 04:00:00 |
+----+--------+---------------------+

My query will return this:

+------------+--------+
| created    | amount |
+------------+--------+
| 2010-03-01 |  50.00 |
| 2010-03-02 | 175.00 |
| 2010-04-01 | 125.00 |
| 2010-04-02 | 100.00 |
+------------+--------+

However, ideally it SHOULD return this:

+------------+--------+
| created    | amount |
+------------+--------+
| 2010-03-01 |  75.00 |
| 2010-03-02 | 150.00 |
| 2010-04-01 | 125.00 |
| 2010-04-02 | 100.00 |
+------------+--------+

The trouble is that if I just subtract a fixed -5, then April is correct but March is not, but if I instead subtract a fixed -6 then March is correct but April is not. What I really need to do is convert to the appropriate time zone in a way that is aware of Daylight Savings Time and can adjust accordingly. Can I do this with SQL query? How do I write this query?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:14:26+00:00Added an answer on May 15, 2026 at 8:14 pm

    None of the current date/time functions are DST aware.

    Using an auxiliary calendar table may be your best bet:

    http://web.archive.org/web/20070611150639/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-calendar-table.html

    You can store UTCOffset’s by date and reference it in your select statement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I keep all of my projects on my laptop. I'm thinking that
Right now I have an SSIS package that runs every morning and gives me
Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
Right now, I'm particularly interested in reading the data from MP3 files (ID3 tags?),
Right now my ant task looks like. <javadoc sourcepath=${source} destdir=${doc}> <link href=http://java.sun.com/j2se/1.5.0/docs/api/ /> </javadoc>
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
Right now I'm making an extremely simple website- about 5 pages. Question is if
Right now, I have two Eclipse projects - they both use Maven 2 for
Right now, my SVN repository is on my laptop's HDD (although I use a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.