sarayucm
sarayucm

Reputation: 126

Date Comparison in If condition in DB2

I have two date parameters BeginDate and EndDate. I need to compare these two dates and then perform some sql select. For example, if BeginDate is lessthan or equal to EndDate, then only I need to query data. For example as below:

If @BeginDate <= @EndDate Then
   select statement1
   select statement2
...
End if

When I tried in DB2, I am getting an error.

Kindly suggest a sample with DB2 syntax.

Upvotes: 0

Views: 1271

Answers (1)

Academia
Academia

Reputation: 4124

In case you want to do it simple, just add the condition to both select statements:

SELECT * 
FROM <table_name> 
WHERE @BeginDate <= @EndDate;

Other solution would be using IF statement within PL/SQL contexts to execute SQL:

IF (@BeginDate <= @EndDate) THEN
  statements
END IF;

A more complete example for inline SQL PL, would be something like:

CREATE PROCEDURE EXAMPLE_IF
(IN BeginDate DATE, IN EndDate DATE)
LANGUAGE SQL
BEGIN ATOMIC
  IF (BeginDate <= EndDate)
    THEN 
      statement1;
      statement2;
  END IF;
END!

Hope it's useful!

Upvotes: 2

Related Questions