Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL query date according to time zone

We are using a Vertica database with table columns of type timestamptz, all data is inserted according to the UTC timezone. We are using spring-jdbc's NamedParameterJdbcTemplate

All queries are based on full calendar days, e.g. start date 2013/08/01 and end date 2013/08/31, which brings everything between '2013/08/01 00:00:00.0000' and '2013/08/31 23:59:59.9999'

We are trying to modify our queries to consider timezones, i.e. I can for my local timezone I can ask for '2013/08/01 00:00:00.0000 Asia/Jerusalem' till '2013/08/31 23:59:59.9999 Asia/Jerusalem', which is obviously different then '2013/08/01 00:00:00.0000 UTC' till '2013/08/31 23:59:59.9999 UTC'.

So far, I cannot find a way to do so, I tried setting the timezone in the session:

set timezone to 'Asia/Jerusalem';

This doesn't even work in my database client.

Calculating the difference in our Java code will not work for us as we also have queries returning date groupings (this will get completely messed up).

Any ideas or recommendations?

like image 750
Sigal Shaharabani Avatar asked Mar 23 '23 13:03

Sigal Shaharabani


1 Answers

I am not familiar with Veritca, but some general advice:

  • It is usually best to use half-open intervals for date range queries. The start date should be inclusive, while the end date should be exclusive. In other words:

    start <= date < end
    

    or

    start <= date && end > date
    

    Your end date wouldn't be '2013/08/31 23:59:59.9999', it would instead be the start of the next day, or '2013/09/01 00:00:00.0000'. This avoids problems relating to precision of decimals.

    That example is for finding a single date. Since you are querying a range of dates, then you have two inputs. So it would be:

    startFieldInDatabase >= yourStartParameter
        AND
    endFieldInDatabase < yourEndParameter
    

    Again, you would first increment the end parameter value to the start of the next day.

  • It sounds like perhaps Vertica is TZ aware, given that you talked about timestamptz types in your answer. Assuming they are similar to Oracle's TIMESTAMPTZ type, then it sounds like your solution will work just fine.

  • But usually, if you are storing times in UTC in your database, then you would simply convert the query input time(s) in advance. So rather than querying between '2013/08/01 00:00:00.0000' and '2013/09/01 00:00:00.0000', you would convert that ahead of time and query between '2013/07/31 21:00:00.0000' and '2013/08/31 21:00:00.0000'. There are numerous posts already on how to do that conversion in Java either natively or with Joda Time, so I won't repeat that here.

  • As a side note, you should make sure that whatever TZDB implementation you are using (Vertica's, Java's, or JodaTime's) has the latest 2013d update, since that includes the change for Israel's daylight saving time rule that goes into effect this year.

like image 197
Matt Johnson-Pint Avatar answered Apr 05 '23 17:04

Matt Johnson-Pint