Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle SQL - DATE greater than statement

As the title says, I want to find a way to check which of my data sets are past 6 months from SYSDATE via query.

SELECT * FROM OrderArchive WHERE OrderDate <= '31 Dec 2014'; 

I've tried the following but it returns an error saying my date format is wrong. However, inserting the data I used that date format as requested/intended and had no issues.

Error at Command Line : 10 Column : 25

Blockquote

Error report -

SQL Error: ORA-01861: literal does not match format string 01861. 00000 - "literal does not match format string"

*Cause: Literals in the input must be the same length as literals in the format string (with the exception of leading whitespace). If the "FX" modifier has been toggled on, the literal must match exactly, with no extra whitespace.

*Action: Correct the format string to match the literal.

like image 841
user3521826 Avatar asked May 31 '15 09:05

user3521826


People also ask

How do you check if one date is greater than another in Oracle?

You have two options here. One option is to convert the date value to a string and do a string comparison as follows: IF (TO_CHAR(date_val,'YYYY') > '2000') THEN END IF; Another option is to use the TO_DATE function to convert the comparison string to a date value.

How does SQL Developer compare dates?

SQL Date comparison is most used statement for DBA or developers. If you will compare any column with a DATE format, related column and data should be DATE datatype, namely SQL date comparison should be between DATE to DATE format as follows.

How can I compare two timestamps in Oracle?

To calculate the difference between the timestamps in Oracle, simply subtract the start timestamp from the end timestamp (here: arrival - departure ). The resulting column will be in INTERVAL DAY TO SECOND . The first number you see is the number of whole days that passed from departure to arrival .


2 Answers

As your query string is a literal, and assuming your dates are properly stored as DATE you should use date literals:

SELECT * FROM OrderArchive WHERE OrderDate <= DATE '2015-12-31' 

If you want to use TO_DATE (because, for example, your query value is not a literal), I suggest you to explicitly set the NLS_DATE_LANGUAGE parameter as you are using US abbreviated month names. That way, it won't break on some localized Oracle Installation:

SELECT * FROM OrderArchive WHERE OrderDate <= to_date('31 Dec 2014', 'DD MON YYYY',                            'NLS_DATE_LANGUAGE = American'); 
like image 182
Sylvain Leroux Avatar answered Oct 06 '22 01:10

Sylvain Leroux


You need to convert the string to date using the to_date() function

SELECT * FROM OrderArchive WHERE OrderDate <= to_date('31-Dec-2014','DD-MON-YYYY'); 

OR

SELECT * FROM OrderArchive WHERE OrderDate <= to_date('31 Dec 2014','DD MON YYYY'); 

OR

SELECT * FROM OrderArchive WHERE OrderDate <= to_date('2014-12-31','yyyy-MM-dd'); 

This will work only if OrderDate is stored in Date format. If it is Varchar you should apply to_date() func on that column also like

 SELECT * FROM OrderArchive     WHERE to_date(OrderDate,'yyyy-Mm-dd') <= to_date('2014-12-31','yyyy-MM-dd'); 
like image 26
Sachu Avatar answered Oct 06 '22 00:10

Sachu