how to convert YYYYMMDD to DD-Mon-YYYY in oracle? - sql

I am trying to convert the date from YYYYMMDD to DD-Mon-YYYY in Oracle, but to_char or to_Date is not working. Can you please advise?
select to_date(20150324,'DD-Mon-YY') from dual;
select to_char(20150324,'DD-Mon-YY') from dual;
I get an error message saying: - ORA-01861: literal does not match format string

Use this combination of to_char and to_date:
select to_char (to_date('20150324','YYYYMMDD'), 'DD-Mon-YY') from dual;
Your mistake was, that you used the wrong date pattern. Additionally it's recommended to add'', though it worked without them in this case.
Check this Fiddle.

Related

How to convert system date to YYYYMM format by using SQL Query?

I need to convert sysdate in YYYYMM format to validate the expiry date, this is the query I have written but am facing the below error.
select to_char (to_date(sysdate,'MM/DD/YYYY'),'YYYYMM') from dual;
ORA-01858: a non-numeric character was found where a numeric was expected
There is no reason to convert sysdate to a date. So just use:
select to_char(sysdate, 'YYYYMM')
If you wanna to format the date, try "DATE_FORMAT" way:
SELECT DATE_FORMAT('2011-11-11 13:14:01','%m%Y')
or you wanna to get the difference between the expired date and sysdate,you may try "DATEDIFF":
SELECT DATEDIFF('2001-01-01','2001-02-02') -> -32

How to convert a given date with a specified date format using dual table on Oracle SQL

Below is the example
2016-02-06T06:06:41+00:00
will be converted to
2/6/2016 6:06:00 AM
Thanks in advance
Use TO_CHAR to get a formatted string from a timestamp:
select to_char(timestamp '2016-02-06 06:06:41 +00:00',
'fmdd/mm/yyyy fmhh:mi:ss am') from dual;
(The FM in the format exist to suppress leading zeros.)

oracle sql query giving error

Following query is giving error:
SELECT to_char(last_day(add_months(to_char(to_date('01-02-2013','dd-mm-yyyy'),
'dd-MON-yyyy'),-1)) + 1,'dd-mm-yyyy') FROM dual;
ORA-01858: a non-numeric character was found where a numeric was expected
I tried this on two systems:
with NLS_DATE_FORMAT='DD-MON-RR' - this query works fine.
With NLS_DATE_FORMAT='MM-DD-YYYY' - gives me error ORA-01858: a non-numeric character was found where a numeric was expected.
Any clues as to why this query is failing? I can't have the queries be dependent on the DATE format.
Why are you doing a to_char when calling add_months , you need to pass a date like
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),
,-1)) + 1,'dd-mm-yyyy') FROM dual;
You have an implicit char-to-date conversion, in the add_months() call; the argument you're passing is a string, not a date. The to_char() you have inside that is redundant, and causing the error when your NLS_DATE_FORMAT doesn't match the format you're using in that to_char():
SELECT to_char(last_day(add_months(to_date('01-02-2013','dd-mm-yyyy'),-1)) + 1,
'dd-mm-yyyy') FROM dual;
I'm not entirely sure what you're doing though... if you want the first day of the month that date is in, you can do this:
SELECT to_char(trunc(to_date('01-02-2013', 'dd-mm-yyyy'), 'MM'),
'dd-mm-yyyy') FROM dual;
This uses the TRUNC(date) function to effectively round down to the start of the month.

How to convert string to date format in Oracle

I have a file contains a string like 2011-03-14 11:57:42+08:00 and I need to store it in a column with DATE datatype. I use Oracle 11g.
I have tried TO_DATE function but it didn't work.
Can anyone help me?
thanks in advance
You have to use the function TO_TIMESTAMP_TZ to get the timezone properly parsed:
SELECT CAST(TO_TIMESTAMP_TZ('2011-03-14 11:57:42+08:00', 'YYYY-MM-DD HH24:MI:SS+TZH:TZM') AS DATE) AS my_date
FROM DUAL
;
(And as a side note to your example in the comment: no need for TO_CHAR, quoting was missing and format model MON (abbreviated month) was incorrect)

Convert number to date sql oracle

I'm trying to convert a number (yyyymmdd) to date (mm/dd/yyyy)
For example
20150302 ====> 03/02/2015
You can try this:
select to_date(20150302,'yyyymmdd') from dual;
or
select to_char(to_date(20150302,'yyyymmdd'),'mm/dd/yyyy') from dual;
You can use TO_DATE function to convert NUMBER to DATE. Try in following:
SELECT TO_DATE(20150302, 'YYYYMMDD') FROM DUAL
The above answer is still incorrect.
It returns a character string when a date is needed.
The correct way is:
select to_date(to_char(20210416), 'YYYYMMDD') num_to_char_to_date from dual;
TO_DATE accepts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype and converts into a value of DATE datatype.
So, convert the number into string, and apply to_date. You could use single-quotation marks around the number to convert it into string.
SELECT TO_DATE('20150302', 'YYYYMMDD') FROM dual;
Remember, a DATE has no format, what you see is for display purpose. If you want to display the date in a desired format, then use TO_CHAR along with your desired format model.
SELECT TO_CHAR(TO_DATE('20150302', 'YYYYMMDD'), 'mm/dd/yyyy') FROM dual;
Read more about TO_DATE.
To convert in the required format:
select to_char(to_date(20150302, 'YYYYDDMM'), 'mm/dd/yyyy')
select to_char(to_date(20220912, 'YYYYDDMM'), 'YYYYMMDD') from dual