Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare date with current_date in Oracle

Tags:

sql

oracle

I can not get what is wrong in this query ( ORACLE QUERY )

SELECT *
FROM HR.CUSTOMER C
dual
WHERE CREATED_AT = current_date
;

I am getting this error

ORA-00933: SQL command not properly ended
like image 993
Manish Champaneri Avatar asked Sep 02 '17 10:09

Manish Champaneri


People also ask

Can you compare dates in Oracle?

You can use "classic comparators" (like >, >=, <, =...) to compare 2 dates. If you have date data types : don't convert them. if you have no choice, use something like: if to_char(d1,'YYYYMMDD') > to_char(d2,'YYYYMMDD') then ... else ...

How can I compare two date formats in SQL?

In SQL, the date value has DATE datatype which accepts date in 'yyyy-mm-dd' format. To compare two dates, we will declare two dates and compare them using the IF-ELSE statement. We can declare variables easily by using the keyword DECLARE before the variable name.

What is difference between CURRENT_DATE and Sysdate in Oracle?

SYSDATE will give the system date of the server. CURRENT_DATE will give you the system date of the session. For example, if your oracle server is in UK, the SYSDATE will be the UK time whereas, the CURRENT_DATE will be the Indian time.


1 Answers

There is a dual too many in your query.

Moreover, in Oracle current_date is not the current date for the database, but the current datetime of your session. While your database server may be in a timezone where it is currently 11 p.m., it may be next day 3 a.m. already on your PC. Whenever you spot current_date in an Oracle query it is very likely wrong.

In Oracle use sysdate for now and trunc(sysdate) for today.

select * 
from hr.customer 
where created_at = trunc(sysdate);
like image 130
Thorsten Kettner Avatar answered Oct 10 '22 00:10

Thorsten Kettner