Frederick
Frederick

Reputation: 1

What is the correct syntax for working with Dates in DB2?

I am writing a SQL statement that may be used against a number of database types. I have never worked with DB2 before, and am unsure of the syntax for working with dates. The dates are stored as a date type. Here is a snippet of the statement against a SQL Server database: SELECT * from tbl1 where YEAR(Start_Time) = YEAR(GetDate() -365)

I thinking that the equivalent in DB2 would be: SELECT * from tbl1 where YEAR(Start_Time) = YEAR(current_date -365 days)

Unfortunately, I don't have a DB2 environment at my home office (thanks to COVID-19)

Upvotes: 0

Views: 709

Answers (1)

Mark Barinstein
Mark Barinstein

Reputation: 12314

Datetime operations and durations

SELECT Start_Time
FROM
(
VALUES 
  CURRENT TIMESTAMP - 365 DAYS - 1 HOUR - 1 MINUTE - 1 SECOND - 1 MICROSECOND
, CURRENT TIMESTAMP - 365 DAYS
) tbl1 (Start_Time) 
WHERE YEAR(Start_Time) = YEAR(CURRENT TIMESTAMP - 365 DAYS);

The result is:

|START_TIME                |
|--------------------------|
|2019-04-08-15.17.17.613999|
|2019-04-08-16.18.18.614000|

Upvotes: 1

Related Questions