Find below some useful information about Time Zone Conversion in oracle. Hope this would be helpful for many of them since all the real time projects that we work in follow different time zones (EST,PST etc) and you might well need to convert them to something specific as per your requirements:
Expand|Select|Wrap|Line Numbers
- insert into dates values(6, to_date('09/20/05 23:15', 'MM/DD/YY HH24:MI'));
- --The contents of the table now look like this:
- 1 09/14/05, 21:08
- 2 09/27/05, 00:00
- 3 10/02/05, 22:05
- 4 09/01/05, 17:01
- 5 09/12/05, 14:30
- 6 09/20/05, 23:15
The date format in Oracle does not contain time zone information, but the database does. To find out the time zone set, execute this query:
Expand|Select|Wrap|Line Numbers
- SELECT dbtimezone FROM dual;
- DBTIME
- ——
- -04:00
Expand|Select|Wrap|Line Numbers
- ALTER database SET TIME_ZONE = '-05:00';
Switching Time Zones
The function new_time is used to convert a time to different time zones. To illustrate this we’ll look at entry 5 from the dates file.
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(entry_date, 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=5;
- entry e_date
- 5 09/12/05 02:30 PM
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(new_time(entry_date, 'EST', 'CST'), 'MM/DD/YY HH:MI AM') e_Date FROM dates WHERE entry=5;
- entry e_date
- 5 09/12/05 01:30 PM
Now let’s grab this time in Pacific time:
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(new_time(entry_date, 'EST', 'PST'), 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=5;
- entry e_date
- 5 09/12/05 11:30 AM
Now we see not only the time converted, but also the time of day has gone from PM to AM.
Now let’s take a look at entry 6:
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(entry_date, 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=6;
- entry e_date
- 6 09/20/05 11:15 PM
We’ll again assume this timestamp is in US Eastern time, but let’s convert it this time to Greenwich Mean Time.
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(new_time(entry_date, 'EST', 'GMT'), 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=6;
- entry e_date
- 6 09/21/05 04:15 AM
Here we convert an entry made in Pacific Time to Eastern:
Code:
Expand|Select|Wrap|Line Numbers
- INSERT INTO dates
- VALUES (7,
- new_time(to_date(’09/22/05 10:28 AM’, ‘MM/DD/YY HH:MI AM’), ‘PST’, ‘EST’));
Expand|Select|Wrap|Line Numbers
- SELECT entry, to_char(entry_date, ‘MM/DD/YY HH:MI AM’) e_date FROM dates WHERE entry=7;
- entry e_date
- 7 09/22/05 01:28 PM