1
SELECT to_date(to_char(SYSTIMESTAMP, 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL;

==> 04-MAR-16

Can anybody explain why this select statement doesn't result in '03/04/2016'?

How can I write my selection so that it does result in this, as a date type? I have also tried

SELECT to_date(to_char(trunc(SYSTIMESTAMP), 'MM/DD/YYYY'), 'MM/DD/YYYY') FROM DUAL

with the same result.

Brian Brock
  • 357
  • 2
  • 3
  • 18

2 Answers2

3

When a date is returned by a query and displayed, it obviously needs to be formatted in some way. The way a date-type value is formatted is not determined by the query, but by the tool that executes your query and displays the result.

In the case of SQL developer you can set that format as follows:

  1. Choose menu Tools > Preferences.
  2. In the Preferences dialog, select Database > NLS from the left panel.
  3. From the list of NLS parameters, enter "MM/DD/YYYY"
  4. Save and close

See also this question.

Note that to convert a timestamp to date you need just to truncate it: trunc(SYSTIMESTAMP). Converting it to string and then back to a date is unnecessary.

Community
  • 1
  • 1
trincot
  • 317,000
  • 35
  • 244
  • 286
0

You are converting a datetime to a string and back to a date. Your system defaults the date format to DD-MMM-YY for output purposes; this is the normal default for date in Oracle.

It is important to understand that the internal data structure for date/time types has nothing to do with how they are presented. So, if you want it in another format, convert to a string using to_char().

If you want to change the default format, then look at NLS_DATE_FORMAT. The documentation is here.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786