Convert Oracle string to date wtih timezone - oracle

I can convert this string to a date
select to_date('2013-10-15T17:18:28', 'YYYY-mm-DD"T"HH24:MI:SS') from dual
But how can I convert this string
'2013-10-15T17:18:28-06:00'
which includes a timezone?

Use TO_TIMESTAMP_TZ instead:
select to_timestamp_tz('2013-10-15T17:18:28-06:00'
,'YYYY-MM-DD"T"HH24:MI:SSTZH:TZM')
from dual;
15/OCT/13 05:18:28.000000000 PM -06:00

Related

Hadoop Hive Date String to UTC Time SQL

I have a String column in my database which looks like
07/12/2019 04:17:08 PM
I use the function
cast(from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss'),'yyyy-MM-dd HH:mm:ss') as timestamp)as mytime
This gives me the result of
2019-07-12 04:17:08.0
I want the result to be in utc format and look something like
2019-07-12 16:17:08.
How can i change this to be in utc format?
Use aaa to parse the AM/PM in datetime. from_unixtime converts it to yyyy-MM-dd hh:mm:ss by default where the hour part is 24 hour format.
from_unixtime(unix_timestamp(myfield, 'MM/dd/yyyy hh:mm:ss aaa'))

I have a varchar value 1st March 2017 that i need as a date 01-MAR-2017 in pl/sql

SELECT TO_DATE('1st March 2017','DD MON YYYY') from dual
It's ok with SELECT TO_DATE(01 March 2017','DD MON YYYY') from dual , doesn't like the 'st'
I think you need something like regex:
SELECT TO_DATE(
regexp_replace('1st March 2017','^(\d+)\w+','\1')
,'DD MON YYYY') from dual
Alas, you can't do it directly: https://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm#BABGDDFB
Notes on date format element suffixes:
When you add one of these suffixes to a datetime format element, the return value is always in English.
Datetime suffixes are valid only to format output. You cannot use them to insert a date into the database.
Which actually means they work only with to_char() to transform a date into a string; they don't work with to_date() to convert a string to a date.
So you will have to play dirty tricks - perhaps regexp_replace to get rid of st. Like Michael has shown already.
Hope here is your answer
SELECT to_date('1st march 2017','dd"st" month yyyy') "Date" FROM dual;
Date
01-MAR-17
SELECT TO_CHAR(to_date('1st march 2017','dd"st" month yyyy'),'dd-mon-yyyy') "Date"
FROM dual;
Date
01-mar-2017

How to covert string to date in oracle

How can I convert the string "2014-03-14 15:15:35 PM EST to date format in Oracle by using 'to-date function. Would apprecaite help
The only way I can think of is to ignore "PM":
select to_timestamp_tz('2014-03-14 15:15:35 PM EST', 'YYYY-MM-DD HH24:MI:SS "PM" TZD')
from dual;
15:15:35 PM - the string contains 24-hour format, but at the same time it contains a "PM" indicator. Oracle cannot understand that. Is that 03:15:35 AM or 03:15:35 PM?

convert oracle date string to number

I need to convert a date string -'2013-01-01' to number --20130101 type..How can I accomplish it in Oracle efficiently?
My Input-
'2013-01-01'
My Output
Output-20130101
select to_number(replace('2013-01-01', '-')) from dual;
You can convert the string to date and convert back using the format that you want to:
select to_char(to_date('2013-01-01', 'YYYY-MM-DD'), 'YYYYMMDD');
More information about datetime format:
http://docs.oracle.com/cd/B19306_01/server.102/b14200/sql_elements004.htm
to_char(your_date,'YYYYMMDD')
Just the basics of using to_char with a date here.
You can also "exploit" the format mask:
select to_number('2013-01-01', '9999G99G99', 'nls_numeric_characters=,-')
from dual
Here is a sqlfiddle demo
To convert date string to number:
Always convert it to char first then to number:
Select TO_NUMBER(TO_CHAR(to_date('2013-01-01', 'YYYY-MM-DD'),'YYYYMMDD')) from dual
if your column datatype is DATE then use your column name in place of sysdate in the below:
Select TO_NUMBER(TO_CHAR(sysdate,'YYYYMMDD')) from dual

Using Oracle to_date function for date string with milliseconds

I have to perform some inserts into an Oracle DB. I have some dates
in the following format
'23.12.2011 13:01:001'
Following the documentation I wrote inserts to_date as follows:
to_date('23.12.2011 13:01:01', 'DD.MM.YYYY HH24:MI:SS')
which works properly. Now I have dates with milliseconds with the format
'23.12.2011 13:01:001'
I've tried the following:
to_date('23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3')
which is incorrect (delivers an error 01821. 00000 - "date format not recognized").
Which "String" should I use for this format with milliseconds?
An Oracle DATE does not store times with more precision than a second. You cannot store millisecond precision data in a DATE column.
Your two options are to either truncate the string of the milliseconds before converting it into a DATE, i.e.
to_date( substr('23.12.2011 13:01:001', 1, 19), 'DD.MM.YYYY HH24:MI:SS' )
or to convert the string into a TIMESTAMP that does support millisecond precision
to_timestamp( '23.12.2011 13:01:001', 'DD.MM.YYYY HH24:MI:SSFF3' )
TO_DATE supports conversion to DATE datatype, which doesn't support milliseconds. If you want millisecond support in Oracle, you should look at TIMESTAMP datatype and TO_TIMESTAMP function.
Hope that helps.
For three digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF3')
For six digits millisecond:
TO_CHAR(LN_AUTOD_UWRG_DTTM,'MM/DD/YYYY HH24:MI:SS.FF'),
You can try this format SS.FF for milliseconds:
to_timestamp(table_1.date_col,'DD-Mon-RR HH24:MI:SS.FF')
For more details:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions193.htm
You have to change date class to timestamp.
String s=df.format(c.getTime());
java.util.Date parsedUtilDate = df.parse(s);
java.sql.Timestamp timestamp = new java.sql.Timestamp(parsedUtilDate.getTime());

Resources