环境:Oracle 10g,11g问题重现:PL/SQL中命令窗口下,发现存储过程得到的时间格式不符合预期要求。SQL> select sysdate from dual;SYSDATE ----------- 2014-12-18Executed in 0 secondsSQL> 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月-14PL/SQL procedure successfully completedExecuted 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-18PL/SQL procedure successfully completedExecuted in 0.016 secondsSQL> SQL> declare pro_date date; begin select sysdate into pro_date from dual; dbms_output.put_line(pro_date); end; /18-12月-14PL/SQL procedure successfully completedExecuted in 0 seconds处理方法2:改变会话的NLS_DATE_FORMATSQL> alter session set nls_date_format="yyyy-mm-dd hh24:mi:ss";Session alteredExecuted in 0.015 secondsSQL> 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:15PL/SQL procedure successfully completedExecuted in 0 secondsSQL> alter session set nls_date_format="yyyy-mm-dd";Session alteredExecuted in 0 secondsSQL> SQL> declare pro_date date; begin select sysdate into pro_date from dual; dbms_output.put_line(pro_date); end; /2014-12-18PL/SQL procedure successfully completedExecuted in 0 seconds总结:在Oracle存储过程想要获取YYYY-MM-DD的时间格式,可以转换成字符串处理,可以临时指定会话的NLS_DATE_FORMAT变量,还可以整体修改客户端的环境变量。更多Oracle相关信息见Oracle 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=12本文永久更新链接地址