oracle convert DD-MON-YY to DD/MM/YYYY - oracle

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

Related

Oracle sqldveloper view date and tora difference

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.

How do I change the default NLS parameters for date format through Toad?

I have a NLS date format as DD-MON-RR. This gives me the underlying date format as YY while I want to change it to YYYY. I tried using the following query and it ran successfully
DECLARE
v_date DATE := sysdate;
BEGIN
DBMS_OUTPUT.put_line(TO_CHAR(v_date, 'MM/DD/YYYY'));
END;
But that didn't change the default format.
for some context, I am trying to import data from Oracle to Tableau. Unfortunately when I try to export a crosstab from Tableau server it looks at the underlying data rather than whats on the view. This causes the date that I have as 25-Jun-2017 to change to 25-Jun-17 in the excel.
The only workaround I have been able to understand is to change the default format of the underlying/source data which in this case is Oracle DB.
I am using TOAD and am trying to understand how can I change it to possibly DD/MON/RRRR format or something similar with 4 digits in the year column.
Any workaround is also appreciated
As already given in other answers you can set NLS_DATE_FORMAT by ALTER SESSION.
In order to set it only for you local PC, open Registry Editor and navigate to HKLM\SOFTWARE\Wow6432Node\ORACLE\KEY_%ORACLE_HOME_NAME%, resp. HKLM\SOFTWARE\ORACLE\KEY_%ORACLE_HOME_NAME%.
There you can add a String Value NLS_DATE_FORMAT, for example:
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\ORACLE\KEY_OraClient11g_home1]
"NLS_TIMESTAMP_FORMAT"="YYYY-MM-DD HH24:MI:SSfmXFF3"
"NLS_TIMESTAMP_TZ_FORMAT"="YYYY-MM-DD HH24:MI:SSfmXFF3 fmTZH:TZM"
"NLS_DATE_FORMAT"="YYYY-MM-DD HH24:MI:SS"
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\ORACLE\KEY_OraClient11g_home1]
"NLS_TIMESTAMP_FORMAT"="YYYY-MM-DD HH24:MI:SSfmXFF3"
"NLS_TIMESTAMP_TZ_FORMAT"="YYYY-MM-DD HH24:MI:SSfmXFF3 fmTZH:TZM"
"NLS_DATE_FORMAT"="YYYY-MM-DD HH24:MI:SS"
You can set NLS_DATE_FORMAT also as an Environment Variable in Windows Settings.
alter session set nls_date_format='DD/MON/RRRR' programmatically in the application or
CREATE OR REPLACE TRIGGER trg_after_logon AFTER LOGON ON DATABASE
BEGIN
execute immediate 'alter session set NLS_DATE_FORMAT=''DD/MON/RRRR''';
END;
in system or sys schema.
Alternatively, you may use
alter system set NLS_DATE_FORMAT='DD/MON/RRRR' scope = both
provided you're in system or sys, again.
Manage your date format masking using the most reasonable approach
First of all, I agree with Alex regarding using to_char. This would be my first choice for modifying date masks for specific requirements.
In Toad on an ad hoc basis, you could just invoke the alter session command as needed:
ALTER SESSION SET nls_date_format='DD/MON/RRRR';
If you are partial to a specific date format mask (and you see yourself often issuing the command, ALTER SESSION SET NLS...) then perhaps you might want to consider changing your user login settings.
If you just modify your specific user preference login file, login.sql (see here ), your session will adhere to the date format mask of your choosing at the beginning of your session. I am partial to creating the environment variable, SQLPATH, and placing my login script there.
Toad will honor your login.sql file settings (e.g. see this post).
Since this is driven by specific requirements or personal preferences, I would never think of modifying this from default at the site level.

change date format 'yyyy/mm/dd' to 'mm-dd-yyyy' in Oracle

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.

Oracle to_date procedure with different results

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.

Oracle Date format [duplicate]

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

Resources