• 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
    2 pro_date date;
    3 begin
    4 select sysdate into pro_date from dual;
    5 dbms_output.put_line(pro_date);
    6 end;
    7 /
    
    18-12月-14
    
    PL/SQL procedure successfully completed
    
    Executed in 0.016 seconds

    处理方法1:将结果转换成字符串格式:

    SQL> 
    SQL> declare
    2 pro_date date;
    3 begin
    4 select sysdate into pro_date from dual;
    5 dbms_output.put_line(to_char(pro_date,'yyyy-mm-dd'));
    6 end;
    7 /
    
    2014-12-18
    
    PL/SQL procedure successfully completed
    
    Executed in 0.016 seconds
    
    SQL> 
    SQL> declare
    2 pro_date date;
    3 begin
    4 select sysdate into pro_date from dual;
    5 dbms_output.put_line(pro_date);
    6 end;
    7 /
    
    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
    2 pro_date date;
    3 begin
    4 select sysdate into pro_date from dual;
    5 dbms_output.put_line(pro_date);
    6 end;
    7 /
    
    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
    2 pro_date date;
    3 begin
    4 select sysdate into pro_date from dual;
    5 dbms_output.put_line(pro_date);
    6 end;
    7 /
    
    2014-12-18
    
    PL/SQL procedure successfully completed
    
    Executed in 0 seconds

    总结:在Oracle存储过程想要获取YYYY-MM-DD的时间格式,可以转换成字符串处理,可以临时指定会话的NLS_DATE_FORMAT变量,还可以整体修改客户端的环境变量。

  • 相关阅读:
    《代码阅读与实践》阅读笔记*part1
    HDFS JAVA API
    《需求工程》阅读笔记*part3
    《需求工程》阅读笔记*part2
    HDFS文件命令
    HOG特征+SVM行人检测
    winchecksec安装踩坑
    加壳:挂起方式创建进程
    内存写入注入
    远程线程注入
  • 原文地址:https://www.cnblogs.com/jyzhao/p/4171367.html
Copyright © 2020-2023  润新知