Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current date time in plsql?

Tags:

oracle

plsql

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.

like image 963
ktaro Avatar asked Dec 06 '18 04:12

ktaro


People also ask

How do you find current date and time in Oracle?

SYSDATE returns the current date and time set for the operating system on which the database resides. The datatype of the returned value is DATE , and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter. The function requires no arguments.

How do I get the current time in SQL Developer?

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE . The time zone offset reflects the current local time of the SQL session. If you omit precision, then the default is 6.

How do I get current date in SQL?

The GETDATE() function returns the current database system date and time, in a 'YYYY-MM-DD hh:mm:ss. mmm' format. Tip: Also look at the CURRENT_TIMESTAMP function.

Which query is used to get current date?

MySQL SYSDATE() Function The SYSDATE() function returns the current date and time.


2 Answers

SYSDATE does have a time component. The first one outputs 06-DEC-18 because your session's parameter NLS_DATE_FORMAT is set to DD-MON-YY.

You have these options.

use TO_CHAR

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

Modify NLS_DATE_FORMAT to show time.

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

Or use SYSTIMESTAMP to show higher precision of time like milliseconds.

BEGIN 
    dbms_output.put_line(SYSTIMESTAMP); 
END;
/
like image 95
Kaushik Nayak Avatar answered Nov 15 '22 07:11

Kaushik Nayak


You can use SYSTIMESTAMP

SELECT SYSTIMESTAMP
FROM DUAL
like image 44
Mihawk Avatar answered Nov 15 '22 06:11

Mihawk