to_char is for converting some value in other data type to a char string
While to_date is used to convert a value to a date type OR to convert a date in one format to another
Main Topics
Browse All Topicswhat is the diff in two?
i know that to_char used to change into character format and to_date in numeric format but what is the logic?
for eg
select to_char(sysdate)
from dual
select to_date(sysdate)
from dual
i will get the same result right?
so when is the use of it?
can u show some examples to clarify me.how do i properly use and when can i use?
cheers!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
v_date date:to_date('12-jun-2003'
how can i initialise date while declaring
i know its works even if i just declare like
v_date date:'12-jun-2003';
but i want to get result as 12/jun/2003 i would like to see the action of to_char changing into character format, is what u mean for function of to_char?
can explain me more.
When you specify "v_date date:='12-jun-2003';" you are using implicit conversion based upon your language set-up, but it in my opionion, it is always preferable to use explicit conversion, especially when dealing with dates.
As to your problem of printing the values, in SQL*Plus:
SQL> set serveroutput on;
SQL> DECLARE
2 l_date DATE := TO_DATE('01/01/2004', 'DD/MM/YYYY');
3 l_date_formatted VARCHAR2(50);
4 BEGIN
5 l_date_formatted := TO_CHAR(l_date, 'Day dth Month');
6 DBMS_OUTPUT.put_line('form
7 END;
8 /
formatted = Thursday 4th January
PL/SQL procedure successfully completed.
SQL>
I can;t initialise only one time, do i need to initialise 2 times like u did in declaration and executable part.
like in varchar when i initialise value dbms_output package simply show my value, i can not use to_char or to_date only one time, i can;t do this task by only one declaration and initialisation
to_char and to_date functions will implicitly use the NLS_DATE_FORMAT setting for the current SESSION, if the format string is not given.
Because we developers cannot control in every case what this will be for all users, countries and applications it is usually sensible to provide the date format when we are manipulating and displaying dates.
alter session set nls_date_format='DD/MM/YYY
set serveroutput on;
DECLARE
l_date DATE := '01/01/2004';
BEGIN
DBMS_OUTPUT.put_line('form
END;
/
formatted = 01/01/2004
PL/SQL procedure successfully completed.
alter session set nls_date_format='MM/DD/YYY
DECLARE
l_date DATE := '31/12/2004';
BEGIN
DBMS_OUTPUT.put_line('form
END;
/
DECLARE
*
ERROR at line 1:
ORA-01843: not a valid month
ORA-06512: at line 2
Strictly you don't NEED formatting, it's just that it is BEST PRACTICE.to use it.
The difference between to_char and to_date is as follows:
TO_DATE: used to convert a number into a varchar2 format eg
SELECT TO_CHAR(123456)
FROM DUAL;
will convert the number 123456 into a varchar2 type string '123456'
TO_DATE: used to convert a number or varchar2 datatype into a date eg
SELECT TO_DATE (12122004, 'DD-MM-YYYY')
FROM DUAL;
will convert the number 12122004 into a data with the format of: 12/12/2004.
When you do:
select to_date(sysdate)
from dual
the result is a date datatype with the format of: dd/mm/yyyy
If however you do:
Select to_char(sysdate)
from dual
the result is a varchar2 datatype with the format: 'DD-MON-YY'
so the results tell you the same but the format and datatype of each is different.
With respect to when to use both have a look at the following example!!!
say we have a string (varchar2) that is '1212Testerstring' and wanted to convert that into a data datatype that reads 12/12/2004 you could do the following:
SELECT TO_CHAR
(TO_DATE (SUBSTR ('1212Testerstring', 0, 4), 'DD/MM'), 'DD/MM/YYYY' ) data_date
FROM DUAL
The substr just extracts the 1st 4 numberic charactures from the string and returns a string '1212'.
We then perform a to_date conversion on the string '1212' to convert the string into a date datatype with format DD/MM so the result would be 12/12.
Finally if you wanted to change the date 12/12 into a varchar2 datatype and change the format of the date then we to_char it to return a string but in the format of dd/mm/yyyy. This will return a varchar2 datatype but with the format '12/12/2004'
Silly example i know but demonstrates how To_CHAR and TO_DATE can both be used and the differnece between them.
Hope that helps
Duncan
sorry, but your answer isnt right
TO_DATE is for converting strings into DATE-Type and not more
if you deliver a different type oracle is trying to convert them automaticaly. in this case you have database defaults used to format this string.
thats also the reason, why you will get same results as you has seen.
you can deliver a format (like 'mm-dd-yyyy HH24:mi:ss') to define how to interprete the string on first argument.
TO_CHAR has two functions
1. converting numbers into strings
this is usage without a format-parameter
2. converting DATE's into strings
for this you have to deliver a format-string like 'mm-dd-yyyy'
Your first select statement
select to_char(sysdate)
from dual
is all right, but you need a mask to format the output, such as
Your first select statement
select to_char(sysdate,'dd-mon-yy
from dual
Your second statement
select to_date(sysdate)
from dual
is incorrect, since sysdate is already a date datatype and does not need to be converted.
I recommend that you visit the Oracle SQL Reference at otn.oracle.com and look at the datatype section.
Anthony Noriega, MSCS, BSSE, OCP
Business Accounts
Answer for Membership
by: grim_toasterPosted on 2004-06-09 at 01:05:29ID: 11267384
to_char is used to convert dates into a character representation, for example, if you want you query to format the date differently:
i.e.
SELECT TO_CHAR(SYSDATE, 'DD/MM/YYYY HH24:MI:SS') FROM dual; --> 09/06/2004 12:57:54
SELECT TO_CHAR(SYSDATE, 'Day ddth Month') FROM dual; --> Wednesday 09th June
The to_date function is used when you have character input, but need it as a date object (for example when the database column is a Date, you need to insert a date)
i.e.
SELECT TO_DATE('01/01/2004', 'DD/MM/YYYY') FROM dual
SELECT TO_DATE('01/01/2004 13:13:13', 'DD/MM/YYYY HH24:MI:SS') FROM dual
Both functions use format masks to specify how the dates/characters should be formatted, I've given you a couple here but there are many more...