SQL
Working with Timezones in Oracle
Calculate the Timezone Offset
SELECT TZ_OFFSET('US/Pacific') FROM DUAL;
The timezone offset can change based on the current time, as this takes into account if the current date is Daylight Saving (DT) or Standard (ST).
So result of
TZ_OFFSET
is never constant, it depends on the current date as well!
Working with the TIMESTAMP WITH TIMEZONE datatype
You can represent the same time in different timezones. The TIMESTAMP WITH TIMEZONE
datatype is tagged already for a particular timezone, you can convert it to move it into a different timezone. Allthough the time is in essence the same, it will look very different when output.
Moving a TIMESTAMP WITH TIMEZONE
to a different Time Zone can be simply done using the following syntax:
datetimetz AT TIME ZONE tz
Where datetimetz is any value of type TIMESTAMP WITH TIMEZONE
, and tz is a string indicating the TZ you want to convert over.
Example:
SELECT tz, TO_CHAR(ts AT TIME ZONE tz,format) AS ts FROM (SELECT SYSTIMESTAMP AS ts FROM dual), (SELECT 'UTC' AS tz FROM dual), (SELECT 'DD-MON-YYYY HH24:MI:SSxFF TZH:TZM (TZD, TZR)' AS format FROM dual);
Note we are using TO_CHAR
, to control the output of the time, by explicitly converting it to a VARCHAR
.
Time Zones
Time Zones can be expressed in two ways:
- As on Offset: '-06:00', which also matches the 'TZH:TZM'
TO_CHAR
format. - or, as Region Name: America/Denver‘
It is important to note that if you need to automatically adjust for Daylight Saving, to make sure to use the Region Name rather than just an offset. An offset is always fixed at the offset provided, and will never adjust for Daylight Saving time.
More information can be found at:
- Wiki: TZ Database
- Wiki: List of TZ Database Timezones
- Wiki: Daylight Saving Time
- Oracle: Datetime Data Types and Time Zone Support
Note that even for the Region Name, in Oracle, there are multiple options for the same region. However sometimes the names and abreviations are conflicting. It is highly recomended to use the official long form as in 'America/Los_Angeles'. If you would use the short form 'PST', it is first of all dubious whether this means only Standard Time, or if it should adjust for Daylight Saving Time. Also the same abreviations might be custom in two totally different areas.
Oracle considders PDT, PST, and America/Los_Angeles to be all synonyms. This means they will all adjust for Daylight Saving Time.
To see a list of official Timezone Names, and their abbreviated alias in Oracle, you can use following Query:
SELECT * FROM gv$timezone_names
Or you can extend this query to lear the exact Offset
SELECT< tzname, tzabbrev, tz_offset_now, tz_offset_later, CASE WHEN tz_offset_now != tz_offset_later THEN 'Y' ELSE 'N' END AS supports_dst FROM ( SELECT tzname, tzabbrev, TO_CHAR((SYSTIMESTAMP AT TIME ZONE tzname) ,'TZH:TZM (TZD)') AS tz_offset_now, TO_CHAR((SYSTIMESTAMP AT TIME ZONE tzname) + INTERVAL '6' MONTH,'TZH:TZM (TZD)') AS tz_offset_later FROM gv$timezone_names )
We did not use the TZ_OFFSET
function to figure out the Timezone Offset, because it is not parameterized, and as a consequence it only works on the current time (now).
Used SQL Functions: SYSTIMESTAMP
, TO_CHAR
One More example:
SELECT tz, TO_CHAR(ts AT TIME ZONE tz ,format) AS ts_now, TO_CHAR((ts AT TIME ZONE tz) + INTERVAL '6' MONTH,format) AS ts_later FROM (SELECT SYSTIMESTAMP AS ts FROM dual), ( SELECT 'UTC' AS tz FROM dual UNION ALL SELECT 'GMT' AS tz FROM dual UNION ALL SELECT 'MST' AS tz FROM dual UNION ALL SELECT 'PST' AS tz FROM dual UNION ALL SELECT 'US/Mountain' AS tz FROM dual UNION ALL SELECT 'America/Denver' AS tz FROM dual UNION ALL SELECT 'America/Phoenix' AS tz FROM dual ), ( SELECT 'DD-MON-YYYY HH24:MI:SSxFF TZH:TZM (TZD, TZR)' AS format FROM dual );
SYSTIMESTAMP
is a TIMESTAMP WITH TIMEZONE
, while SYSDATE
is just of type DATE
which does not carry Timezone information. SYSDATE
is always in the timezone of the Database.
Date Calculations
If the datatype is of type DATE
, you can operate date calculations on it by simply adding +1
for one day, or +1/24
for one hour (and other fractions). If you are working with a TIMESTAMP WITH TIMEZONE
data type however, the correct way is to use an INTERVAL
.
The interval was already used in previous examples too:
SELECT TO_CHAR((SYSTIMESTAMP AT TIME ZONE 'UTC')+INTERVAL '6' MONTH,'DD-MON-YYYY HH24:MI:SSxFF TZH:TZM (TZD, TZR)') FROM dual
Conversion
It was already shown how you can convert either DATE
or TIMESTAMP WITH TIMEZONE
to a VARCHAR
by means of the TO_CHAR
function.
You can also convert a SYSDATE
into a TIMESTAMP WITH TIMEZONE
:
First Cast it to a TIMESTAMP
, and then apply the appropriate TZ using the oracle FROM_TZ
function to get a TIMESTAMP WITH TIMEZONE
. After having it in a TIMESTAMP WITH TIMEZONE
data type, you move it to different Timezones using the AT TIME ZONE
syntax.
A TIMESTAMP
(with or without TZ), you can convert back to simple type of DATE
using a Cast.
SELECT CAST( FROM_TZ( CAST( (TRUNC(SYSDATE)-1) AS TIMESTAMP), 'US/Pacific' ) AT TIME ZONE 'UTC' AS DATE ) FROM DUAL
Used SQL Functions SYSDATE
, TRUNC
, FROM_TZ
Functions returning current time or date
The details are in the fine print of the documentation. Take a look at the Return Type, and the actual Timezone the DATE
or TIMESTAMP
is calculated in.
SYSDATE
- Return Type: DATE
- Time Zone: Host OS of Database Server
CURRENT_DATE
- Return Type: DATE
- Time Zone: Session
SYSTIMESTAMP
- Return Type: TIMESTAMP WITH TIME ZONE
- Time Zone: Host OS of Database Server
CURRENT_TIMESTAMP
- Return Type: TIMESTAMP WITH TIME ZONE
- Time Zone: Session
LOCALTIMESTAMP
- Return Type: TIMESTAMP
- Time Zone: Session
DBTIMEZONE
- Time Zone: DB Time Zone. Inherits from DB Server OS, but can be overridden using set at DB Creation or Alter using Database Parameter
SET TIME_ZONE=...
. This affects the time zone used forTIMESTAMP WITH LOCAL TIME ZONE
datatypes.
- Time Zone: DB Time Zone. Inherits from DB Server OS, but can be overridden using set at DB Creation or Alter using Database Parameter
SESSIONTIMEZONE
- Time Zone: Session Time zone. Inherits from Session hosting OS, but can be overridden using Session Parameter
ALTER SESSION SET TIME_ZONE=...
.
- Time Zone: Session Time zone. Inherits from Session hosting OS, but can be overridden using Session Parameter
Return Type, indicates whether or not the Timezone is available within the Datatype. If you try to print TZR if datatype does not carry TimeZone, then it will just show up as +00:00 (doesn’t mean it is GMT). Otherwise It will show the TimeZone matching either the Database or Session as indicated.
Time Zone, indicates in which Timezone the time is calculated. For matching TimeZone, the same Date/Time will be shown (HH24:MI).
Note that none of the Functions return the time in the Time Zone set with the Database Time Zone (or as returned by the DBTIMEZONE
function). That is, none of the functions also return a datatype of TIMESTAMP WITH LOCAL TIME ZONE
. However you can convert the output of any of the functions that does return a timezone into a different timezone (including DBTIMEZONE
) as follows:
SELECT SYSTIMESTAMP AT TIME ZONE DBTIMEZONE FROM DUAL;
Related articles
- Conversion methods for Timestamp With Time Zone (memoryisolation.wordpress.com)
Generate a List of Dates
There are many methods to accomplish the same. A few are listed. It probably serves more as a demo to some of the oracle features.
Method 1 – PLSQL Loop
You can also simply build a loop in PL/SQL to generate the list of dates. However for this to work in regular SQL, it will have to be defined using an Oracle ‘Pickler’ or PIPELINED
Function.
More info can be found in “Developing and Optimizing Pipelined Functions”.
Method 2 – MODEL Queries
Oracle has the lesser known feature of Model Queries.
Those type of queries allow accessing and modifying oracle SQL output like it is Excel.
The Model allows you to modify the generated data by accessing cells like excel cells to:
- access it for read,
- access existing cells/rows for changing (UPDATE)
- access existing + non existing (UPDATE + INSERT = UPSERT), allowing you to create additional data that didn’t exist before.
SELECT i,stamp FROM (SELECT 1 AS i,TRUNC(SYSDATE) AS tstamp FROM DUAL) MODEL DIMENSION BY (i) MEASURES (tstamp) RULES UPSERT SEQUENTIAL ORDER ( tstamp[FOR i FROM 2 TO 10 INCREMENT 1] = tstamp[i=cv(i)-1]+1 );
MEASURES
defines the columns you want to access in an array fashion (like you would access a multidimensional array in java or other programming languages.
DIMENSION BY
lists out what the indexes are you want to use for accessing the array.
The RULES defines what the edits are that need to take place.
So with above example we can in combination with a loop build a range of dates, starting from one single preexisting date.
Method 3 – Recursive Subquery Factoring
WITH t AS ( -- Anchor Query (first Row) SELECT TRUNC(SYSDATE) AS tstamp FROM DUAL -- Recursive Query (subsequent Rows) UNION ALL SELECT t.tstamp+1 AS stamp FROM t WHERE t.tstamp+1 <= TRUNC(SYSDATE)+10 ) SELECT * FROM t
Method 4 – Hierarchical Queries using CONNECT BY
SELECT TRUNC(SYSDATE) + (LEVEL-1) AS tstamp FROM DUAL CONNECT BY LEVEL <= 10
Elegance in Simplicity …
References
- Oracle-Developer.net article on improving performance with pipelined table functions
- Oracle 11gR2 Documentation on
pivot
Ever saw oracle’s official documentation on the SQL PIVOT ? Couldn’t find the matching documentation for it, and no examples? I have turned the PIVOT inside-out, and will share you what it does.
Starting with pages of unrelated data points
SELECT student, subject, minor, quarter, score, score_max FROM metric
student | subject | minor | quarter | score | score_max |
Jeff | Math | Algebra | 1 | 14 | 20 |
Jeff | Math | Trigonometry | 1 | 14 | 20 |
Jeff | English | Grammar | 2 | 9 | 10 |
Jeff | English | Literature | 2 | 9 | 10 |
… |
This table basically stores for each student his scores on a couple of subjects, further subdivided into minor subject, and for each individual quarter of the year.
Now, lets try to apply a full fledged PIVOT, including all whistles and bells …
SELECT * FROM ( SELECT student, subject, minor, score, score_max FROM metric ) PIVOT ( COUNT(DISTINCT quarter) AS "CNT", SUM(score) AS num, SUM(score_max) AS den FOR (subject,minor) IN ( ('Math','Trigonometry') AS "M_TRI", ('Math','Algebra') AS "M_ALG", ('English','Grammar') AS "E_GRA", ('English','Literature') AS "E_LIT" ) )
And again the output … maybe to some surprises …
student | m_tri_cnt | m_tri_num | m_tri_den | m_alg_cnt | m_alg_num | m_alg_den | e_gra_cnt | e_gra_num | e_gra_den | e_lit_cnt | e_lit_num | e_lit_den |
Jeff | 2 | 28 | 40 | 2 | 14 | 20 | 4 | 69 | 100 | 3 | 356 | 400 |
Mieke | 3 | 38 | 40 | 2 | 18 | 20 | 4 | 69 | 100 | 4 | 301 | 400 |
… |
Ok, we all expected rows be turned into columns based on some determinator. But a lot more happened, that is obvious. I will list out –
FOR (determinators)
identifies the determinators used to identify the pivoted columns, in this case a combination of two columns(subject,minor)
.IN (...)
lists out any combination of the determinators, and the associated alias (the new column name).- The first part within the
PIVOT (aggregators FOR ...)
, the aggregators (MIN
,MAX
,COUNT
, …). These aggregators basically describe a method to be used to collapse multiple rows into one. In the simplified case, the chosen aggregation method wont matter, But in this example it does. Any column not used in the aggregator sector, will be part of an implicitGROUP BY
. When providing multiple aggregators, you need to associate each aggregator with it’s own alias. This alias will be concatenated at the end of the alias provided for the determinator alias.
English is a very ambiguous medium to try to explain something like this, but this is why the provided examples should shed light on the obscurities. Don’t forget to keep the Oracle SQL Reference Guide handy.
We started with the most complex example. Now to show that it does not need to be always that verbose, we will simplify this example. We will assume:
- All scores are 100-based, and to come to a total score we can just
SUM(...)
- There are no minor subjects anymore
- No quarters either, just a list of students and their score for each main subject
SELECT student, subject, score FROM metric
student | subject | score |
Jeff | Math | 80 |
Jeff | English | 93 |
… |
Now, if we want to pivot this, it becomes much more simpler, and less verbose:
SELECT * FROM metric PIVOT ( SUM(score) FOR (subject) IN ('Math','English') )
What simplified this is the following:
- With a single aggregator, no more need for a separate alias suffix.
- Single determinator.
- The list of determinator combinations does not have an alias defined either. However Columns will appear literally as the expression, showing ‘Math’, including the single quotes.
- The pivoted query is now just the table itself, no subquery.
student | ‘Math’ | ‘English’ |
Jeff | 212 | 534 |
Joe | 365 | 653 |
John | 456 | 543 |
Jo-Ann | 383 | 645 |
… |