tags:

views:

1915

answers:

7

Oracle's default date format is YYYY-MM-DD. Which means if I do:

 select some_date from some_table

...I lose the time portion of my date.

Yes, I know you can "fix" this with:

 alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

But seriously, why isn't the above the default? Especially in a DBMS where the two primary time-tracking data types (DATE and TIMESTAMP) both have a time component that includes (at least) accuracy down to 1 second.

+2  A: 

I'm not an Oracle user (well, lately anyhow), BUT...

In most databases (and in precise language), a date doesn't include a time. Having a date doesn't imply that you are denoting a specific second on that date. Generally if you want a time as well as a date, that's called a timestamp.

Nick Bastin
Nick, thanks for the reply. I agree with you conceptually. It seems Oracle's two types, DATE vs TIMESTAMP, both have a time component. It's just that the TIMESTAMP goes down to more granularity (sub 1-second). http://it.toolbox.com/blogs/database-solutions/a-comparison-of-oracles-date-and-timestamp-datatypes-6681 You would think that the inclusion of a time component in both types would be reason to not have a YYYY-MM-DD display format.
Aaron F.
@Nick: Oracle's DATE type has a one-second precision. It's badly named.
skaffman
@skaffman: It apparently isn't *that* badly named if they have the default string format set to only include the date portion and separately have a higher resolution TIMESTAMP data type. Of course I don't know what their actual recommendation is, but that seems a pretty clear distinction to me on proposed usage. (As always, of course, things with databases are never so clear...)
Nick Bastin
+1  A: 

A DATE value per the SQL standard is YYYY-MM-DD. So even though Oracle stores the time information, my guess is that they're displaying the value in a way that is compatible with the SQL standard. In the standard, there is the TIMESTAMP data type that includes date and time info.

Harold L
+3  A: 

The biggest PITA of Oracle is that is does not have a default date format!

In your installation of Oracle the combination of Locales and install options has picked (the very sensible!) YYYY-MM-DD as the format for outputting dates. Another installation could have picked "DD/MM/YYYY" or "YYYY/DD/MM".

If you want your SQL to be portable to another Oracle site I would recommend you always wrap a TO_CHAR(datecol,'YYYY-MM-DD') or similar function around each date column your SQL or alternativly set the defualt format immediatly after you connect with

ALTER SESSION 
SET NLS_DATE_FORMAT = 'DD-MON-YYYY HH24:MI:SS';

or similar.

James Anderson
The DBA can set up whatever NLS_DATE_FORMAT system-wide that they want. However, often the database simply returns a value in the seven byte internal date format, and the client software does its own job of translating that into a string.
Gary
+2  A: 

The format YYYY-MM-DD is part of ISO8601 a standard for the exchange of date (and time) information.

It's very brave of Oracle to adopt an ISO standard like this, but at the same time, strange they didn't go all the way.

In general people resist anything different, but there are many good International reasons for it.

I know I'm saying revolutionary things, but we should all embrace ISO standards, even it we do it a bit at a time.

pavium
+3  A: 

It's never wise to rely on defaults being set to a particular value, IMHO, whether it's for date formats, currency formats, optimiser modes or whatever. You should always set the value of date format that you need, in the server, the client, or the application.

In particular, never rely on defaults when converting date or numeric data types for display purposes, because a single change to the database can break your application. Always use an explicit conversion format. For years I worked on Oracle systems where the out of the box default date display format was MM/DD/RR, which drove me nuts but at least forced me to always use an explicit conversion.

David Aldridge
+6  A: 

Are you sure you're not confusing Oracle database with Oracle SQL Developer?

The database itself has no date format, the date comes out of the database in raw form. It's up to the client software to render it, and SQL Developer does use YYYY-MM-DD as its default format, which is next to useless, I agree.

edit: As was commented below, SQL Developer can be reconfigured to display DATE values properly, it just has bad defaults.

skaffman
In SQL Developer, Tools > Preferences > Database > NLS Parameters ... there are thirteen parameters that can be set for controlling defaults.
David Aldridge
Oh, version 1.5.5 that is.
David Aldridge
Fair enough, it can be configured, but why the downvote? My point still stands that this is an aspect of the client software, not the database, which was the point of the question.
skaffman
I think that the principle of not relying on defaults is universal and not tool dependent, so i didn;t think it was very relevant.
David Aldridge
+4  A: 

If you are using this query to generate an input file for your Data Warehouse, then you need to format the data appropriately. Essentially in that case you are converting the date (which does have a time component) to a string. You need to explicitly format your string or change your nls_date_format to set the default. In your query you could simply do:

select to_char(some_date, 'yyyy-mm-dd hh24:mi:ss') my_date
  from some_table;
Dougman