Oracle中不同語言環境導致to_date出錯的問題,oracleto_date

來源:互聯網
上載者:User

Oracle中不同語言環境導致to_date出錯的問題,oracleto_date

      寫了個預存程序,裡面用了個函數,函數裡有一段to_date(dateFrom, 'yyyy/mm/dd'),運行後探索資料插入錯誤,插入的資料為“0001/9/14”。感覺莫名其妙,不知道為什麼會是這樣的資料,經過調試,發現我在測試視窗輸入的日期“2014/9/1”在程式中變成了“01-SEP-14”,瞬間恍然大悟,原來是當做01年9月14日了。

      知道了原因就好說了,直接放出解決辦法:to_date(to_char(dateFrom, 'yyyy/mm/dd'), 'yyyy/mm/dd')。再次運行,成功記錄。


oracle插入日期出錯

環境變數 nls_date_language 沒有設定。

下面是 沒有設定的時候 查詢的結果:
SQL> select to_char(sysdate, 'dd-mon-yy') from dual;

TO_CHAR(SYSDAT
--------------
05-10月-11

查詢設定的語句
SQL> show parameter nls_date_language;

NAME TYPE VALUE
------------------------------------ ----------- -------
nls_date_language string

修改設定的語句
SQL> alter session set nls_date_language = 'AMERICAN';

會話已更改。

修改設定以後的查詢
SQL> select to_char(sysdate, 'dd-mon-yy') from dual;

TO_CHAR(SYSD
------------
05-oct-11

SQL> SELECT TO_DATE('05-oct-11', 'dd-mon-yy') from dual;

TO_DATE('05-
------------
05-OCT-11
 
教大蝦ORACLE to_date問題

肯定是因為某筆記錄的格式不符合YYYYMMDD。
你應該寫個function,找出這種異常資料,並糾正。
create function is_valid_date(p_yyyyymmdd in varchar2) return number is
l_date date;
begin
l_date = to_date(p_yyyyymmdd,'yyyymmdd');
return 1;
exception when others then
return 0;
end;

然後寫select語句。
select * from pc_xx where is_valid_date(substr(rydjsj,1,8)) = 0;
 

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.