0% found this document useful (0 votes)
23 views

Conversion Function

The document discusses various Oracle date functions - TO_CHAR, TO_DATE, TO_TIMESTAMP, TO_NUMBER, EXTRACT, and CAST. TO_CHAR converts dates, numbers or timestamps to character strings. TO_DATE converts character strings to dates. TO_TIMESTAMP converts strings to dates with times. TO_NUMBER converts strings to numbers. EXTRACT extracts specific date fields like year, month from dates. CAST converts between data types.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Conversion Function

The document discusses various Oracle date functions - TO_CHAR, TO_DATE, TO_TIMESTAMP, TO_NUMBER, EXTRACT, and CAST. TO_CHAR converts dates, numbers or timestamps to character strings. TO_DATE converts character strings to dates. TO_TIMESTAMP converts strings to dates with times. TO_NUMBER converts strings to numbers. EXTRACT extracts specific date fields like year, month from dates. CAST converts between data types.

Uploaded by

kvigneshk2001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

CONVERSION FUNCTION:

TO_CHAR
TO_DATE
TO_TIMESTAMP
TO_NUMBER
EXTRACT
CAST

TO_CHAR:

(i)It is accept two arguments.


(ii)TO_CHAR function is used to convert a value of a date, number, or timestamp
data type to a character string.

SYNTAX:
TO_CHAR(value, format)

value --- The value you want to convert to a character string(ie


date,number,timestamp)
format --- How the value should be displayed as a string.

select TO_CHAR(sysdate, 'YYYY-MM-DD') from dual;

YYYY : Four-digit year.


YEAR : Year in text(Twenty Twenty-Three)

MM : Month (01-12).
MON :JAN
MONTH:JANUARY

DD : Date of the month (01-31).


DAY : Day of the week (Sunday,Monday,..Saturday).
DY : SUN,MON,TUE
HH : Hours(1-12)
MI : Minutes (00-59).
SS : Seconds (00-59).
HH24 : Hour of the day in 24-hour format (00-23).
AM or PM: AM/PM indicator for 12-hour time format.

select TO_CHAR(sysdate, 'YEAR') from dual;


select TO_CHAR(sysdate, 'DD MONTH YYYY') from dual;
select TO_CHAR(sysdate, 'HH:MI:SS') from dual;
select TO_CHAR(sysdate, 'HH24:MI:SS') from dual;
select TO_CHAR(sysdate, 'HH:MI:SS:FF') from dual; --- date format not
recognized
select TO_CHAR(systimestamp, 'HH:MI:SS:FF') from dual;
select TO_CHAR(systimestamp, 'DD-MON-YYYY HH:MI:SS:FF') from dual;

select TO_CHAR('14-04-10','DD-MM-YY') from dual; --invalid number

/*Here we do not give hard coded value like this '14-04-10' and
our input date should match with oracle default date format. So we use TO_DATE()
function. After then we convert Desired format Using TO_CHAR()*/

select TO_CHAR(TO_DATE(SYSDATE,'DD-MM-YY'),'DDth MONTH, YYYY') from dual;


select TO_CHAR(TO_DATE(HIRE_DATE,'DD-MM-YY'),'DDth MONTH, YYYY') from employees;
select TO_CHAR(TO_DATE('14-04-10','DD-MM-YY'),'DD-MM-YY') from dual;
select TO_CHAR(TO_DATE('14-apr-01','dd-mon-yy'),'DDD') from dual;

select hire_date,TO_CHAR(TO_DATE(HIRE_DATE,'DD-MM-YY'),'DD DAY MONTH, YYYY')


joining_date from employees;
select * from employees where TO_CHAR(hire_date,'dy')='mon';
select TO_CHAR(hire_date,'dy') Joining_day,E.* from employees E where
TO_CHAR(hire_date,'dy')='mon';

TO_DATE:

(i)It is accept two arguments.


(ii)TO_DATE function is used to convert a character string to oracle default date
format(DD-MM-YY).

select 'september 24 1999',TO_DATE('september 24 1999','month dd yyyy') from dual;


select '24/09/1999',TO_DATE('24/09/1999','dd/mm/yyyy') from dual;
select '24.09.1999',TO_DATE('24.09.1999','dd.mm.yyyy') from dual;

SYNTAX:
TO_DATE(string, format)

select TO_DATE('05-15-23','MM-DD-YY') from dual;


select TO_DATE('14-05-23','DD/MM/YY') from dual;
select TO_DATE('14-05-23','DD.MM.YY') from dual;

(to maintain same date format accross the table)

select * from v$version;

create table t1(id number,name varchar2(30),joining_date date);


insert into t1 values(20,'bala','10-12-1977');
insert into t1 values(30,'catherine','28-apr-21');
insert into t1 values(40,'Harry','18-september-21');
select * from t1;
insert into t1 values(70,'Dravid',TO_DATE('24-june-23','DD-MM-YY'));
insert into t1 values(90,'Ezhil',TO_DATE('07-august-10','DD-MM-YY'));
insert into t1 values(42,'Gokul',TO_DATE('18-september-15','DD-MM-YY'));

TO_TIMESTAMP:

(i)It is accept two arguments.


(ii)TO_TIMESTAMP function is used to convert a string values to date with time.

SYNTAX:
TO_TIMESTAMP(string, format)

select systimestamp from dual;


select '09-09-23 1:46:51', TO_TIMESTAMP('09-09-23 1:46:51','DD:MM:YY HH-MI-SS-AM')
from dual;

TO_NUMBER:
(i) It accept one or two arguments.
(ii)It is used to convert string values to Number.

SYNTAX:
TO_NUMBER(string, format)
EXTRACT:
EXTRACT function is used to extract specific field (such as year, month, day, etc.)
from a date.

SYNTAX:
EXTRACT(field FROM source)

YEAR
MONTH
DAY
HOUR
MINUTE
SECOND

select EXTRACT(month from hire_date) joining_june,E.* from employees E where


EXTRACT(month from hire_date)=6;
select ,hire_dateTO_CHAR(hire_date,'mon') from employees E where
TO_CHAR(hire_date,'mon')='jun'

select EXTRACT(Year from hire_date),count(EXTRACT(Year from hire_date)) from


employees group by EXTRACT(Year from hire_date);

select TO_CHAR(hire_date,'yyyy'),count(TO_CHAR(hire_date,'yyyy')) from employees


group by (TO_CHAR(hire_date,'yyyy'));

select TO_CHAR(hire_date,'mon'),E.* from employees E where


TO_CHAR(hire_date,'mon')='jun';

SELECT column_name, data_type FROM user_tab_columns WHERE table_name = 'STUDENT';

CAST:
CAST function converts one data_type to another data_type.

Syntax:
CAST(expression AS target_data_type);

select CAST( '11-jan-22' AS DATE ) from dual;


select CAST( '11-jan-22' AS TIMESTAMP ) from dual;
select CAST('123' AS NUMBER) from dual;
select CAST('123' AS NUMBER)+1 from dual;

select hire_date,CAST(hire_date AS TIMESTAMP) from employees;


select hire_date,CAST(hire_date AS VARCHAR2(30)) from employees;

You might also like