Data loss by date, minute, and second during Oracle Storage

Source: Internet
Author: User
Tags sql using

Data loss by date, minute, and second during Oracle Storage

Today, a developer called me to say that Oracle encountered a strange phenomenon. In the stored procedure, values of date type are lost in minutes, minutes, And seconds. Let's 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 ,'
-------------------
 
17:47:22
 
SQL> CREATE OR REPLACE PROCEDURE test_p
Is
S_ SQL varchar2 (500 );
S_date date;
Begin
S_date: = to_date ('2017-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 ,'
-------------------
00:00:00 -- we can see that the time, minute, and second have been lost. It is suspected that it is caused by implicit conversion.
 

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


SQL> CREATE OR REPLACE PROCEDURE test_p
Is
S_ SQL varchar2 (500 );
S_date date;
Begin
S_date: = to_date ('2017-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 = '18-August 14-14'
The call is complete.
 
-- If implicit conversion is performed, bind the variable.
 
SQL> CREATE OR REPLACE PROCEDURE test_p
Is
S_ SQL varchar2 (500 );
S_date date;
Begin
S_date: = to_date ('2017-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 ,'
-------------------
17:24:32

Use JDBC to call Oracle stored procedures and functions in Java

Local compilation of Oracle stored procedures

Use of Oracle stored procedures and REF CURSOR

Solution to prompt "insufficient Permissions" during Oracle Storage

Oracle uses the stored procedure to return the result set development report

Tips for using temporary tables in Oracle stored procedures

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.