472,353 Members | 2,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 472,353 developers and data experts.

Convertion of Time Zones in Oracle

amitpatel66
2,367 Expert 2GB
Hi All,

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
  1. insert into dates values(6, to_date('09/20/05 23:15', 'MM/DD/YY HH24:MI'));
  2.  
  3. --The contents of the table now look like this:
  4.  
  5. 1 09/14/05, 21:08
  6. 2 09/27/05, 00:00
  7. 3 10/02/05, 22:05
  8. 4 09/01/05, 17:01
  9. 5 09/12/05, 14:30
  10. 6 09/20/05, 23:15
  11.  
  12.  
Changing Time Zones

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
  1.  
  2. SELECT dbtimezone FROM dual;
  3.  
  4. DBTIME
  5. ——
  6. -04:00
  7.  
The time zone can be updated with the command:

Expand|Select|Wrap|Line Numbers
  1. ALTER database SET TIME_ZONE = '-05:00';
  2.  
where you can specify the offset from Greenwich mean time or a valid time zone from the list in the v$timezone_names view. Note that this is one of the few of the ‘v$’ views which are plural.

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
  1. SELECT entry, to_char(entry_date, 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=5;
  2.  
  3. entry       e_date    
  4. 5             09/12/05 02:30 PM
  5.  
  6.  
This database is in US Eastern time but we want to display the time in US Central.

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT entry, to_char(new_time(entry_date, 'EST', 'CST'), 'MM/DD/YY HH:MI AM') e_Date FROM dates WHERE entry=5;
  3.  
  4. entry       e_date
  5. 5             09/12/05 01:30 PM
  6.  
  7.  
Here we clearly see the time converted to Central. Note that the new_time function is performed on the date field, not on the to_char.

Now let’s grab this time in Pacific time:

Expand|Select|Wrap|Line Numbers
  1. SELECT entry, to_char(new_time(entry_date, 'EST', 'PST'), 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=5;
  2.  
  3. entry       e_date
  4. 5            09/12/05 11:30 AM
  5.  
  6.  

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
  1.  
  2. SELECT entry, to_char(entry_date, 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=6;
  3.  
  4. entry       e_date
  5. 6             09/20/05 11:15 PM
  6.  

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
  1.  
  2. SELECT entry, to_char(new_time(entry_date, 'EST', 'GMT'), 'MM/DD/YY HH:MI AM') e_date FROM dates WHERE entry=6;
  3.  
  4. entry       e_date
  5. 6             09/21/05 04:15 AM
  6.  
  7.  
This shows not only the change in hours, but that the date of this entry is displayed properly for its time zone. Of course the new_time function can be used on inserts in the same way. This is useful if you are allowing input from people in different geographical regions.

Here we convert an entry made in Pacific Time to Eastern:


Code:
Expand|Select|Wrap|Line Numbers
  1.  
  2. INSERT INTO dates
  3. VALUES (7,
  4. new_time(to_date(’09/22/05 10:28 AM’, ‘MM/DD/YY HH:MI AM’), ‘PST’, ‘EST’));
  5.  
  6.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT entry, to_char(entry_date, ‘MM/DD/YY HH:MI AM’) e_date FROM dates WHERE entry=7;
  3.  
  4. entry       e_date
  5. 7             09/22/05 01:28 PM
  6.  
So we have converted 10:28 AM Pacific to 1:28 PM Eastern so all our entries in the table are consistent. Of course when performing the insert we need to put the to_date function within the new_time function so the text string is converted to a date format before we try to convert it.
Dec 30 '08 #1
0 19151

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript...
11
by: lduperval | last post by:
Hi, I`m trying to do date calculations in three types of time zones: local, GMT and specified. The issue I am facing is that I need to be able to...
1
by: heirou | last post by:
I'm a novice in this subject....I've made a database that requires a time conversion. For example, if local time is 1200, determine the time in...
6
by: Rebecca Smith | last post by:
Today’s question involves two time text boxes each set to a different time zone. Initially txtCurrentTime will be set to Pacific Time or system...
0
by: Sabotage | last post by:
Hi all, I am looking to download time zones list for my multilingual software. I found time zones list in english here:...
24
by: ChaosKCW | last post by:
Hi I am reading from an oracle database using cx_Oracle. I am writing to a SQLite database using apsw. The oracle database is returning utf-8...
13
by: maflatoun | last post by:
Hi, I have the following function to convert UTC time to Local time. It works perfect for GMT- (Minus) time zones however it provides incorrect...
3
by: Satish Itty | last post by:
Hi all, I have a big problem in my hands and not sure how I can fix this. Any suggestions would be greatly appreciated. I have a .NET 3 tier app...
10
by: WebCM | last post by:
There is a function: http://paste.ubuntu.com/21865 It needs GMT date in YYYY-MM-DD HH:MM:SS format - in SQL: datetime. If date is the same as...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.