简体   繁体   中英

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, eg 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, ie 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?

Okay, so apparently:

set time zone to 'Asia/Jerusalem';

worked and I just didn't realize it, but for the sake of helping others I'm going to add something else that works:

select fiels at time zone 'Asia/Jerusalem' from my_table;

will work for timestamptz fields

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM