Oracle預存程序擷取YYYY-MM-DD的時間格式

來源:互聯網
上載者:User

Oracle預存程序擷取YYYY-MM-DD的時間格式

環境:Oracle 10g,11g

問題重現:PL/SQL中命令視窗下,發現預存程序得到的時間格式不符合預期要求。

SQL> select sysdate from dual;

SYSDATE
-----------
2014-12-18

Executed in 0 seconds

SQL> set serveroutput on
SQL> declare
pro_date date;
begin
select sysdate into pro_date from dual;
dbms_output.put_line(pro_date);
end;
/

18-12月-14

PL/SQL procedure successfully completed

Executed in 0.016 seconds

處理方法1:將結果轉換成字串格式:

SQL>
SQL> declare
pro_date date;
begin
select sysdate into pro_date from dual;
dbms_output.put_line(to_char(pro_date,'yyyy-mm-dd'));
end;
/

2014-12-18

PL/SQL procedure successfully completed

Executed in 0.016 seconds

SQL>
SQL> declare
pro_date date;
begin
select sysdate into pro_date from dual;
dbms_output.put_line(pro_date);
end;
/

18-12月-14

PL/SQL procedure successfully completed

Executed in 0 seconds

處理方法2:改變會話的NLS_DATE_FORMAT

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

Session altered

Executed in 0.015 seconds

SQL>
SQL> declare
pro_date date;
begin
select sysdate into pro_date from dual;
dbms_output.put_line(pro_date);
end;
/

2014-12-18 11:18:15

PL/SQL procedure successfully completed

Executed in 0 seconds

SQL> alter session set nls_date_format='yyyy-mm-dd';

Session altered

Executed in 0 seconds

SQL>
SQL> declare
pro_date date;
begin
select sysdate into pro_date from dual;
dbms_output.put_line(pro_date);
end;
/

2014-12-18

PL/SQL procedure successfully completed

Executed in 0 seconds

總結:在Oracle預存程序想要擷取YYYY-MM-DD的時間格式,可以轉換成字串處理,可以臨時指定會話的NLS_DATE_FORMAT變數,還可以整體修改用戶端的環境變數。

相關文章

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.