Converting date format SQL using convert() - sql

I have the date in the format of yy-mm-dd and I want to convert it to dd-mm-yyyy. I used the following select statement:
select convert(varchar(30), hiredate, 110)
from emp;
But, I keep getting an error that there's a missing expression:
ORA-00936: missing expression
Can someone please guide me?

If the hiredate's data type is DATE, then use to_char function:
select to_char(hiredate, 'dd-mm-yyyy') from emp;
If the hiredate's data type is VARCHAR2 or CHAR, convert it to DATE by to_date then use to_char:
select to_char(to_date(hiredate, 'yy-mm-dd'), 'dd-mm-yyyy') from emp;

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

Can the TO_CHAR in ORACLE HAVE (TO_CHAR(04-28-2017, 'yyyy')) INSTEAD OF Passing a Column name as TO_CHAR(sysdate, 'yyyy/mm/dd')

In general, we pass a column name to the TO_CHAR CLause as TO_CHAR(sysdate, 'yyyy/mm/dd').
I want to know whether can we pass a date in the place of column name as (TO_CHAR(04-28-2017, 'yyyy'))
No, you can't because TO_CHAR function expects either number or date/time value as it's parameter. This can be tested easily.
SQL> select to_char('12-04-2017','DD-MM-YYYY') from dual;
select to_char('12-04-2017','DD-MM-YYYY') from dual
*
ERROR at line 1:
ORA-01722: invalid number
You need to convert the string to date.
SQL> select to_char(to_date('12-04-2017','DD-MM-YYYY'),'YYYY') from dual;
TO_C
----
2017
OR
SQL> select to_char(date '2017-04-12','YYYY') from dual;
TO_C
----
2017

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

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