Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sysdate and current_date have different types?

Tags:

oracle

The oracle docs for sysdate and current_date claim they both return DATEs:

  • sysdate
  • current_date

This test, though:

alter session set plsql_warnings = 'ENABLE:ALL';
create table test(x date);
create or replace procedure test1 authid definer is
    cursor s is select x from test where current_date > x;
begin for x in s loop null; end loop; end;
/
show errors
drop table test;
drop procedure test1;

produces this output:

Errors for PROCEDURE TEST1:
LINE/COL  ERROR
3/42      PLW-07204: conversion away from column type may result in sub-optimal query plan

Using sysdate does not give the same warning. I suspect that substituting current_date for sysdate in queries runs a risk of altering the query plan, especially if the date columns are indexed.

edit:

select dump(current_date) from dual;
select dump(sysdate) from dual;

gives:

DUMP(CURRENT_DATE)
Typ=13 Len=8: 223,7,7,9,11,23,55,0

DUMP(SYSDATE)
Typ=13 Len=8: 223,7,7,9,11,23,55,0
like image 325
Alistair Bayley Avatar asked Jul 08 '15 21:07

Alistair Bayley


People also ask

What is the difference between CURRENT_DATE and Sysdate?

sysdate is the current date on the server where database resides. current_date is the date of the client from where you are connecting. current_date is a function and sysdate is not. Becuse both your server date time and your client data time are same, you are getting the result as 0.

What is the difference between Sysdate and Sysdate?

SYSDATE is a SQL function that returns the current date and time set for the operating system of the database server. CURRENT_DATE returns the current date in the session time zone.

What datatype is Sysdate?

SYSDATE returns the current date and time set for the operating system on which the database server resides. The data type of the returned value is DATE , and the format returned depends on the value of the NLS_DATE_FORMAT initialization parameter.

What is the difference between now () and CURRENT_DATE () in mysql?

Current_date() will only give you the date. now() give you the datetime when the statement,procedure etc... started. sysdate() give you the current datetime.


1 Answers

1) CURRENT_DATE returns the current date in the session time zone. Do you really need current_date? If not, stick with sysdate. That will work your procedure

2) If you still need CURRENT_DATE, following is the solution. Store the value of current_date into variable and it will resolve your problem. Let me if this answers your question.

drop table test;
create table test(x date);
create or replace procedure test1 authid definer 
is
dateVar date;
cursor s is select x from test where dateVar > x;
begin 
dateVar:=current_date;
for x in s loop null; 
end loop; 
end;
/
SQL> show errors
No errors.
like image 181
Nilesh Deshpande Avatar answered Oct 17 '22 05:10

Nilesh Deshpande