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

江枫思渺然 提交于 2021-02-11 15:27:36

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/45018641/how-do-i-change-the-default-nls-parameters-for-date-format-through-toad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!