This question already has an answer here:
comparing date with a predefined format pl sql
(1 answer)
Closed 9 years ago.
Oracle 11g documentation says that default date format is DD-MON-YYYY, which means that if I insert date to a date column using:
insert into table t values(1, '02-JAN-2013')
and then select it
select * from t
it should display as 02-JAN-2013 whereas in reality it displays date as 01/02/2013.
Why his discrepancy or am I missing something.
Ref. http://infolab.stanford.edu/~ullman/fcdb/oracle/or-time.html
FYI. I am using PL/SQL Developer for this experiment.
There isn't really a default date format for the product, the reference guide states that the default for NLS_DATE_FORMAT at system level is based on NLS_TERRITORY. Though this says the 'standard' is `DD-MON-RR', and I think that's what you get if you don't specify a value at database level, at least on the platforms I've worked on.
When you create a new database the initialisation parameters can include a specific NLS_DATE_FORMAT, and that can be changed later too.
But the system-level NLS_DATE_FORMAT can be overridden by the client, the session, or in a TO_CHAR call. You can look in v$nls_parameters to see the system value, and nls_session_parameters to see your current session value. You can change that with alter session if you want to.
I'm pretty sure you'll find that PL/SQL Developer is setting the session NLS_DATE_FORMAT to MM/DD/YYYY somewhere in its preferences.
Generally it's better to not rely on that value at all, and always use an explicit format mask for display, e.g. TO_CHAR(<column>, 'YYYY-MM-DD HH24:MI:SS').
There's more on NLS_DATE_FORMAT in the globalisation guide here and here; and a bit about date display here; and an overview of the date format model elements here
It depends of NSL_DATE_FORMAT that depends of NLS_TERRITORY
Give a look to: http://docs.oracle.com/cd/B19306_01/server.102/b14237/initparams122.htm
Related
application when connecting to oracle, at session level it sets the format 'YYYY-MM-DD HH24:MI:SS', v$parameter also has the same format.
while calling a procedure, the date argument is passed like '2022-07-06 10:54:06'. most of the time it works but sometimes we get "SQLCODE = -1861".
the problem continues until that session is closed by an application restart.
At this moment it is suspected the NLS_DATE_FORMAT was changed for that session(its just a guess).
adding to_date to the date argument while calling the procedure would most likely resolve the problem,
putting a trigger to capture the NLS_DATE_FORMAT at logon time also may not help as it could be changed after login.
so question is, is there a way to get the NLS_DATE_FORMAT of all sessions on an instance? or is there a way to change the NLS_DATE_FORMAT of an already active session from another session?
is there a way to get the NLS_DATE_FORMAT of all sessions on an instance?
For your own session (which is what you actually want, based on the comment), you'd query v$nls_parameters:
SQL> select * from v$nls_parameters where parameter = 'NLS_DATE_FORMAT';
PARAMETER VALUE
-------------------- --------------------
NLS_DATE_FORMAT dd.mm.yyyy hh24:mi
SQL>
As of you viewing information about other sessions, you can't do that easily - see what Sergiusz Wolicki from Oracle says:
(...) this information is not easily available. NLS environment is kept in an UGA variable and other sessions to not have access to it. Skilled Oracle engineers would be able to get this information from an UGA dump but otherwise it is not retrievable.
I'm not one of "skilled Oracle engineers" so ... I'm afraid I can't help any further, sorry.
If I try a query as
select * from hr.employees
by sqldeveloper I have an output where the field HIRE_DATE is display so:
21-GIU-07
The data format is in according of the land (Italy)
If I use Tora or Toad (an old version) for the query the same row and the same field is display as
HIRE_DATE 2007-06-21 00:00:00
I am not undestand why there is time value in the field HIRE_DATE. In the example is 0 but I have found table where the time is set.
Why Tora/Toad show the time too and not in sqldeveloper ?
Thanks in advance anyone wants to answer
There is a parameter called NLS_DATE_FORMAT which specifies the default format for date data type.
The NLS_DATE_FORMAT has an order of overriding precedence, and tool specific NLS paramter settings will override it. That is the reason why two different tools has different outputs.
This is the most usual order of overriding precendence :
NLS_DATE_FORMAT is in the database initialization parameters, will be overriden by,
Settings of OS environment variable on the client machine, will be overriden by,
NLS parameter setting at session level with ALTER SESSION statements, will be overriden by,
to_date and to_char functions at the sql statement level.
Having said all that, in your situation, you need to check the NLS_DATE_FORMAT in both the tools.
Last but most important, check this link and learn more about the NLS_DATE_FORMAT.
I am trying to convert the format of a varchar2 column from 'DD-MON-YY' to 'DD/MM/YYYY'.
In example: from '01-JAN-16' to '01/01/2016'
In case you can ask or it may help:
'MON' part is in English however my current NLS settings are in Turkish.
All the years are after 2000.
How can I do this?
Thanks in advance..
If you don't provide the NLS_DATE_LANGUAGE parameter, your own session's parameter will be used.
You can override that like so:
select TO_CHAR(TO_DATE('01-JAN-16','DD-MON-YY', 'NLS_DATE_LANGUAGE = English'),
'DD/MM/YYYY') from dual;
This will affect only this query, nothing else. If you need to work with many dates like this,
ALTER SESSION SET NLS_DATE_LANGUAGE='ENGLISH'
- then you can change it back later, or it will reset to Turkish when this session ends and you start another session.
If you need this change to be made (almost) permanent, put it in your settings in SQL Developer or Toad, or the login.sql for SQL*Plus.
Try this:
TO_CHAR(TO_DATE('01-JAN-16','DD-MON-YY'),'DD/MM/YYYY')
Your data must be clean - everything must conform to the original format or you'll encounter errors on the TO_DATE conversion.
Go to Tools —> Preferences —> Database —-> NLS —> Date Format and change date to DD/MM/YYYY .
Oracle SQL Developer Version 18.4.0.376 Build 376.1900
I have inserted into a table in Oracle. My implementation without PLSQL would be:
SELECT to_date('1900-01-01','YYYY-MM-DD') + (rownum - 1) AS DT_CAL,
rownum AS NUM_JOUR
FROM dual
CONNECT BY to_date('1900-01-01','YYYY-MM-DD') + (rownum - 1) <=
to_date('2000-12-31','YYYY-MM-DD')
result is: 05/28/1900, not 1900-05-28. Can you help me understand what the problem is?
The DATE data type does not have a format; Oracle stores it as either 7- or 8-bytes and it is not until it is passed to a client program (i.e. SQL/Plus, SQL Developer, Toad, Java, Python, etc) and that client program formats it according to whatever rules it has that the date gets a format.
If you are using SQL/Plus or SQL Developer then it will use the NLS_DATE_FORMAT session parameter to format the date. You can change this using:
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';
(Be aware that this will only change the format in the current session and will not change it for any other sessions/users.)
If you want to give the date a particular format then you will need to convert it to a string.
to_date() takes your string parameter, matches it to the format you provide in the second parameter, and constructs a date field from it. The date field isn't using the format you provided in the second parameter - in fact it'll be stored using some internal data representation that has no format at all (a number, in all likelihood).
To present a format back out in the results from a date field, you can either:
Have the client executing the query set the NLS parameters (at session level) to provide a localized format, with an ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'; statement), or
Use to_char(..., 'YYYY-MM-DD') around your existing field to turn the date back into a string formatted the way you want to have it. Where you replace ... with your current column definition in the select.
Approach #1 is already happening, as there'll already be an NLS_DATE_FORMAT set that is producing the current format, but it's with a format you don't want, so if you can control it and change it there, you can do it that way. If you can't and you must have the format a single consistent other way, then #2 could be the way to go.
I'm calling an Oracle procedure with this code:
begin
PPT2000_AGG_HOURLY.RUNDURATION_HOURLY(4, to_date('01112014', 'DDMMYYYY'), 20);
end;
/
My problem is the date format. On one PC the procedure calculates all fine with the result for the date 01.11.2014 00:00.
On the other PC (same version of Oracle SQL Developer) the procedure returns
0 for the calculation and for the date 01.11.2014
So something seems to be different in converting the date and I don't know why. I tried it with a third PC and get also the return of 01.11.2014.
The PC which gives the correct result has the same operating system (WIN7 German) and the same version of the Oracle SQL Developer. Also the same Oracle database.
So how could this be possible?
Update Solution:
Thanks for the answers, I changed the NLS-Settings from DD.MM.RR to DD.MM.RR HH24:MI and now it works :)
You should handle the display format in the code, and not depend on the client's NLS_DATE_FORMAT. You can't expect 1000 users to change their local NLS settings in their GUI based client tools.
A date doesn't have a format. What you see, is just for display depending on the locale-specific NLS_DATE_FORMAT of the client.
And the NLS_DATE_FORMAT could be overridden at different levels. If you don't want to depend on the client's NLS settings, then always use TO_CHAR with desired format mask to make sure it is overridden at individual statement level.
Remember, for diaplaying DATE values, always use TO_CHAR with desired format. To do date arithmetic, use TO_DATE to convert a literal into DATE.
For example,
TO_CHAR(date_column, '<desired_format>')
It will override the locale-specific NLS_DATE_FORMAT at statement level.
See this similar answer for more understanding.
If you really have a constant date then use the Oracle DateTime literal. It works every time and it's independent of the NLS_DATE_FORMAT:
begin
PPT2000_AGG_HOURLY.RUNDURATION_HOURLY(4, DATE '2014-11-01', 20);
end;
/
Check the NLS preferences in SQL Developer to see if there is a mismatch. Go to Preferences->Database->NLS. You can also override these settings on a per session basis if you want.