Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SYSDATE() without DUAL table

I'm surprised this question hasn't been asked. I must be missing something very obvious here, and I'm prepared for all the down votes and the 3 second answer.

In MSSQL you're able to call GETDATE() without having to depend on a database table but in Oracle SYSDATE has to come from DUAL. Is there no way to get the current date in Oracle without having to go through DUAL?

EDIT

Clearly, this doesn't have much to do with SYSDATE but more with my incorrect usage of it. See the following WHERE clause:

WHERE (extract(YEAR from ORDER_DATE) = (case when extract(month from SYSDATE()) < 11 then extract(year from SYSDATE()) else extract(year from SYSDATE())+1 end)

The above will not work, and from the answers here it seems that adding dual in there is not recommended. What's the most optimal solution here?

like image 234
jgozal Avatar asked Oct 20 '25 15:10

jgozal


1 Answers

Avoid writing () after sysdate.

Example - using your CASE in the WHERE clause (without using DUAL):

create table T ( dt date ) ;

insert into T ( dt ) values ( sysdate ) ;


-- just write SYSDATE, not SYSDATE()
select *
from T   
where ( extract ( year from dt ) ) = ( 
  case 
    when extract( month from sysdate ) < 11 then
      extract( year from sysdate ) 
    else
      extract( year from sysdate ) + 1
  end 
) ;

-- output
DT       
---------
23-MAY-19

DBfiddle

like image 119
stefan Avatar answered Oct 23 '25 04:10

stefan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!