oracle 如何在plsql中获取当前日期时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53644486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 05:35:54  来源:igfitidea点击:

How to get the current date time in plsql?

oracleplsql

提问by ktaro

My Code:

我的代码:

DECLARE

BEGIN 

    -- outputs 06-DEC-18
    dbms_output.put_line(sysdate);  

    -- also outputs 18-DEC-06
    dbms_output.put_line(to_date(sysdate,'yyyy-mm-dd hh24:mi:ss')); 
END;
/

The output only shows the date. I want to get also the time.

输出仅显示日期。我也想得到时间。

采纳答案by Kaushik Nayak

SYSDATEdoes have a time component. The first one outputs06-DEC-18because your session's parameter NLS_DATE_FORMATis set to DD-MON-YY.

SYSDATE确实有时间成分。第一个输出06-DEC-18是因为您的会话参数NLS_DATE_FORMAT设置为DD-MON-YY.

You have these options.

你有这些选择。

use TO_CHAR

TO_CHAR

dbms_output.put_line(TO_CHAR(SYSDATE,'yyyy-mm-dd hh24:mi:ss'));

Modify NLS_DATE_FORMATto show time.

修改NLS_DATE_FORMAT以显示时间。

ALTER SESSION SET NLS_DATE_FORMAT='yyyy-mm-dd hh24:mi:ss';
BEGIN
   dbms_output.put_line(SYSDATE); 
END;
/

Or use SYSTIMESTAMPto show higher precision of time like milliseconds.

或用于SYSTIMESTAMP显示更高的时间精度,如毫秒。

BEGIN 
    dbms_output.put_line(SYSTIMESTAMP); 
END;
/

回答by Mihawk

You can use SYSTIMESTAMP

您可以使用 SYSTIMESTAMP

SELECT SYSTIMESTAMP
FROM DUAL

回答by Maxim

To convert date type to string with some format use to_char

要将日期类型转换为具有某种格式的字符串,请使用to_char

to_char(sysdate::timestamp, 'YYYY-MM-DD HH24:MI:SS')