How to covert string to date in oracle - 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?

Related

why the date info of minute was wrong after conversion

I used SQL to convert a date:
select date,to_char(date,'yyyy/mm/dd HH24:mm') from process
The original date is 12/5/2018 2:41:06 PM
but the conversion result is 2018/12/05 14:12.
Is my SQL wrong?
mm is the placeholder for the month - regardless of the position. So the second mm contains the month again.
As documented in the manual the placeholder for minutes is mi
So you need: to_char(date,'yyyy/mm/dd HH24:MI')

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'))

Oracle giving ORA-01821 on a format that appears to be valid

Calling
SELECT TO_DATE('Mon Sep 22 18:02:41 CDT 2014', 'DY MON DD HH24:MI:SS TZD YYYY') FROM Dual;
I get
ORA-01821: date format not recognized
01821. 00000 - "date format not recognized"
I put together the format string from these Oracle instructions. I tried both "TZR" and "TZD", neither works.
DY Abbreviated name of day.
MON Abbreviated name of month.
DD Day of month (1-31).
HH24 Hour of day (0-23).
MI Minute (0-59).
SS Second (0-59).
TZD Daylight savings information. For example, 'PST'
TZR Time zone region.
YYYY 4-digit year
Why is this not working?
TO_DATE doesn't support timezone.
I think you need to use to_timestamp_TZ() to do what you're after...
SELECT To_TimeStamp_TZ('Mon Sep 22 18:02:41 2014 CDT', 'DY MON DD HH24:MI:SS YYYY TZD')
FROM Dual;
Gives you something like (with my NLS paramaters)
22-SEP-14 06.02.41.000000000 PM AMERICA/CHICAGO
Also note the data type must be TIMESTAMP WITH TIME ZONE Data Type; or oracle just drops the timezone information w/o error.
https://docs.oracle.com/cd/E11882_01/server.112/e10729/ch4datetime.htm#NLSPG238

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

Convert Oracle string to date wtih timezone

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

Resources