Use date, minutes, seconds to lose in Oracle stored procedures

Source: Internet
Author: User

Today, a development brother to find me, said that there is a strange phenomenon, in the stored procedure to assign a date type of value, time, minutes, seconds are lost, the following to do an experiment:

sql> drop table test purge;

sql> CREATE TABLE test
(
Fill_date Date
);
sql> INSERT into test values (sysdate);
Sql> commit;
Sql> Select To_char (fill_date, ' Yyyy-mm-dd HH24:mi:ss ') from test;
To_char (Fill_date, '
-------------------

2014-07-18 17:47:22

sql> CREATE OR REPLACE PROCEDURE test_p
Is
S_sql VARCHAR2 (500);
S_date date;
Begin
s_date:= to_date (' 2014-07-18 17:24:32 ', ' yyyy-mm-dd HH24:mi:ss ');
S_sql: = ' UPDATE test SET fill_date = ' ' | | S_date | | ";
Execute immediate s_sql;
Commit
End

/

Sql> call Test_p ();

Sql> Select To_char (fill_date, ' Yyyy-mm-dd HH24:mi:ss ') from test;
To_char (Fill_date, '
-------------------
2014-07-18 00:00:00 --you can see that the real time, minute, and second have been lost, the suspicion is that the implicit conversion caused

--Adjust the stored procedure to see what is printed out.

sql> CREATE OR REPLACE PROCEDURE test_p
Is
S_sql VARCHAR2 (500);
S_date date;
Begin
s_date:= to_date (' 2014-07-18 17:24:32 ', ' yyyy-mm-dd HH24:mi:ss ');
S_sql: = ' UPDATE test SET fill_date = ' ' | | S_date | | ";
Dbms_output.put_line (S_sql);
Execute immediate s_sql;
Commit
End

/

Sql> set Serveroutput on

Sql> call Test_p ();
UPDATE Test SET fill_date = ' 1 August-July-14 '
The call is complete.

--to confirm that an implicit conversion has occurred, the binding variable is used

sql> CREATE OR REPLACE PROCEDURE test_p
Is
S_sql VARCHAR2 (500);
S_date date;
Begin
s_date:= to_date (' 2014-07-18 17:24:32 ', ' yyyy-mm-dd HH24:mi:ss ');
S_sql: = ' UPDATE test SET fill_date =: 1 ';
Execute immediate s_sql using S_date;
Commit
End

/

Sql> call Test_p ();
The call is complete.

Sql> Select To_char (fill_date, ' Yyyy-mm-dd HH24:mi:ss ') from test;
To_char (Fill_date, '
-------------------
2014-07-18 17:24:32

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.