I have a field in my table with datatype as DATE in Oracle. I want to insert the current date into that field, in format DD/MM/YYYY format.
I tried the below query:
select to_date(to_char(sysdate,'dd/mm/yyyy'),'dd/mm/yyyy') from dual
But it gives
1/8/2011 12:00:00 AM.
I want it to insert and show as
08/01/2011 12:00:00 AM.
Can anyone help me in this please ?
When the column is of type DATE, you can use something like:
This removes the time part from SYSDATE.
Alternatively, if you want to retrieve the date part of the DATE field, you may use truncate, i.e.
Maybe this can help
DATE
is a built-in type in Oracle, which is represented in a fixed way and you have no control over it.So:
The above is nonsensical. You don't insert a string, you insert a date.
Format is useful only when you want:
TO_DATE
(format mask: how to parse the string);TO_CHAR
(format mask: how to render the date).So basically, in your example you take a DATE, you convert it to a STRING with some format, and convert it back to DATE with the same format. This is a no-op.
Now, what your client displays: this is because your Oracle Client won't display DATE fields directly and the NLS layer will convert any DATE field that is selected. So it depends on your locale by default.
What you want is
SELECT TO_CHAR(SYSDATE,'DD/MM/YYYY') FROM dual;
which will explicitly perform the conversion and return a string.And when you want to insert a date in a database, you can use
TO_DATE
or date literals.