Convert number to date sql oracle - sql

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

Related

How to transform date from P20210301 to DD.MM.YYYY in oracle

I have a date column P20210301. How to tranform this date to format 01.03.2021?
This can be done by converting your text to a date datatype using TO_DATE, then converting it to the format you want as a string with TO_CHAR
SELECT TO_CHAR (TO_DATE ('P20210301', '"P"YYYYMMDD'), 'DD.MM.YYYY') AS new_date FROM DUAL;
Database by default date stored in yyyy-mm-dd format so you have to manage a coding side
Note :if you use a PHP language then you use a date() function in PHP.
if any other language use like this:
TO_CHAR ( TO_DATE (str, 'YYYY/MM/DD'), 'DD.MM.YYYY')
Happy Coding...!!

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

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

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.

Date conversion in Oracle not working as expected

ORACLE 10 g db: convert date format from
02-JUL-14
to
02/JUL/14
I tried using the the select query below to get "02/JUL/14" instead it shows "02-JUL-14":
SELECT ROUTINGNUM , to_date (EFFDATE,'DD/MM/YYYY') FROM hat;
Can anybody please help reading this.
if EFFDATE is a date column,
SELECT ROUTINGNUM , to_char( EFFDATE,'DD/MON/YYYY') FROM hat;
if it is a String in the format DD-MON-YY
SELECT ROUTINGNUM , to_char( to_date( EFFDATE,'DD-MON-YY') ,'DD/MON/YYYY')
FROM hat;
TO_DATE function is used for converting from String to Date type. Use TO_CHAR function instead.
If your EFFDATE field is already a date type, then you just need to change NLS_DATE_FORMAT.
alter session set NLS_DATE_FORMAT = 'DD/MM/YYYY';
select eff_date from hat;
Or use TO_CHAR on EFFDATE
select to_char(eff_date, 'DD/MM/YYYY') from hat;
If you typically deal with a certain format of date, then you can change the default for your instance by setting one or more of the NLS init parameters.
sqlplus / as sysdba
show parameter nls