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 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;
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;
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.
- 3 SQL tuning ideas on Table Columns to apply
- DB2 derived Table key knowledge Points you need to learn
- DB2 DATE example SQL Queries to your project