Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing DB2 dates

Tags:

db2

I have a DB2 DATE type field in a DB2 table. I want to select data by a date filter. For example:

SELECT *
FROM   table
WHERE registrationdate > '2002-10-01';

From the above query, I get records with registrationdate starting from '1943-10-01', but this is incorrect.

These do not work either:

registrationdate > date('2002-10-01')
date(registrationdate) > date('2002-10-01')
date(registrationdate) > '2002-10-01'

How do I need to compare dates?

like image 864
yons88 Avatar asked May 28 '12 16:05

yons88


People also ask

How do I find the difference between two dates in DB2?

Answer. The Date/Time wizard (found under Transform -> Date/Time) can calculate the difference between two dates with results in days, months or years.

How can I compare two dates in SQL?

This can be easily done using equals to(=), less than(<), and greater than(>) operators. 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.

Can timestamp be compared?

A date, time, or timestamp value can be compared with another value of the same data type, a datetime constant of the same data type, or with a string representation of a value of that data type. Additionally, a TIMESTAMP WITHOUT TIME ZONE value can be compared with a TIMESTAMP WITH TIME ZONE value.

Can we compare timestamp in SQL?

Answer. The SQL99 (SQL3) Standard states that the DATE, TIME, and TIMESTAMP data types are not compatible for either assignment or comparison. DB2 for Linux, UNIX, and Windows and WebSphere Federation Server enforce this restriction.


1 Answers

The SQL standard format for a DATE literal is:

DATE '2002-10-01'

At the very least, it is worth trying:

SELECT *
  FROM table
 WHERE registrationdate > DATE '2002-10-01';
like image 97
Jonathan Leffler Avatar answered Oct 22 '22 06:10

Jonathan Leffler