Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp/date time from UTC to EST Oracle SQL

I have a field with a date/time value like this:

2009-11-17 18:40:05

It's in UTC. In the query how can I convert this to EST?

I'm trying something like this but it throws an error.

// datetime is the field name
SELECT 
   FROM_TZ(TIMESTAMP TO_DATE(datetime, 'yyyy-mm-dd hh24miss'), 'EST') AS DT
FROM
   db_name
like image 703
Phill Pafford Avatar asked Nov 17 '09 19:11

Phill Pafford


People also ask

How do I convert UTC time to local time in SQL?

SELECT CONVERT(datetime, SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETUTCDATE()), DATENAME(TZOFFSET, SYSDATETIMEOFFSET()))) AS LOCAL_IST; Here, the GETUTCDATE() function can be used to get the current date and time UTC. Using this query the UTC gets converted to local IST.

How do I change timezone in Oracle query?

Here's an example of setting TIME_ZONE to such values: ALTER SESSION SET TIME_ZONE=local; ALTER SESSION SET TIME_ZONE=dbtimezone; ALTER SESSION SET TIME_ZONE='Canada/Eastern'; ALTER SESSION SET TIME_ZONE='-04:00'; You can check your current session's time zone with the SESSIONTIMEZONE function.

Does Oracle timestamp have timezone?

Introduction to Oracle TIMESTAMP WITH TIME ZONE Its default value is 6. Oracle considers two TIMESTAMP WITH TIME ZONE values are equal if they represent the same value in UTC regardless of the time zone data.


1 Answers

I had to tweak it slightly to get it to work on my database, but this worked:

select from_tz(to_timestamp('2009-11-17 18:40:05','yyyy-mm-dd hh24:mi:ss'), 'UTC') 
at time zone 'America/New_York' from dual

The key is the "at time zone" syntax.

like image 77
Dan Avatar answered Oct 29 '22 15:10

Dan