DB2 Date SQL Queries to Use in Your Current Project

DATE SQL Queries

In SQL, DATE and TIME are popular functions. In business, all the scenarios you need to deal with DATE and Time. Top examples to compare DATE and TIME given in this post for your project reference.

SQL Query to get System Date

You can get system date using sysibm.sysdummy1. Those are current date current time and current time zone you can get fro sysibm.sysdummy1.

Extract Command on DATE and Time

You can use extract command to extract date, day, month portion from Timestamp. This extract command you can use in WHERE or ORDER BY clause.

SELECT ORDER_DATE, ORDER_TOTAL
FROM ORDER_TABLE
WHERE EXTRACT(MONTH FROM ORDER_DATE)=9;
SELECT ORDER_TOTAL
FROM ORDER_TABLE
WHERE YEAR(ORDER_DATE)=2000;
Manners in Australia
Manners in Australia

SQL TO_CHAR function to convert DATE to CHAR

TO_CHAR(ORDER_DATE,YY-MM-DD)
This function converts the Order date to Char date.

Tricky SQL Last Day of the Month in DB2

SELECT ORDER_DATE, DATE(SUBSTR(CHAR(ORDER_DATE),1,8) ||'01' + 1 MONTH - 1 DAY AS LAST_DAY_OF_MONTH
FROM ORDER_TABLE;

DB2 Limitation

When you calculate differences of Timestamp, it does not consider leaf year. Always DB2 consider, 30 days per month

Differences of DATE and Time

You can find examples to find differences for DATE and TIME for your project, this is my post on it useful for you to refer.

Related Posts

Advertisements

Author: Srini

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