DB2: How to Compare Dates

Suppose you get a chance to write a DATE comparison in the WHERE clause. Here’re brilliant ideas on how to use it in the WHERE clause for your quick reference.

Advertisements

For this demo, I chose three examples. This logic you can follow for comparing the DATEs in your project.

SQL Query Compares Dates using ‘>’ Greater than

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

The syntax for DATE is

datecolumn > ‘XXXX-XX-XX’

  • Always you need to give date in quotes.
  • You can use <, >, = in DATE comparisons.

SQL Query Compare Dates using ‘<‘ Less than

SELECT *
FROM table
WHERE purchdate < '2002-10-10';

SQL Query Using DATE ‘=’ Equal to

SELECT *
FROM table
WHERE purchdate = '2020-04-19;

Related posts

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.