Handouts - 14 (Date Fun)
Handouts - 14 (Date Fun)
Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Session-14
The Teradata DATE data type is used to store calendar dates representing year, month and
day. It is stored internally as a four-byte integer.
DATE Arithmetic
Find Syntax
SELECT DATE +
The date 30 days from today 30;
SELECT DATE +
The date 1 year from now 365;
ADD_MONTHS
The ADD_MONTHS function allows the addition of a specified number of months to an
existing date, resulting in a new date.
The format of the function is: ADD_MONTHS (date, n)
where n is a number or an expression representing the number of months to be added to
the date.
The following examples demonstrate its usage.
Query Results
SELECT DATE; /* March 20, 2001 */ 01/03/20
SELECT ADD_MONTHS (DATE, 2) 2001-05-20
SELECT ADD_MONTHS (DATE, 12*14) 2015-03-20
SELECT ADD_MONTHS (DATE, -3) 2000-12-20
Note: The results of the ADD_MONTH function are always displayed in YYYY-MM-DD
Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL
format.
ADD_MONTHS may also be applied to a literal date, which must be specified in the YYYY-
MM-DD format.
Query Results
SELECT ADD_MONTHS ('1996-07-31', 2) 1996-09-30
SELECT ADD_MONTHS ('1995-12-31', 2) 1996-02-29
SELECT ADD_MONTHS ('1995-12-31', 14) 1997-02-28
ADD_MONTHS accounts for the Gregorian calendar and knows how many days are in each
month, including leap years such as 1996.
Scenario:Show the date two month from today (March 20, 2001) using both ADD_MONTHS
and simple arithmetic methods.
SELECT DATE ,ADD_MONTHS(DATE, 2) ,DATE + 60;
Query Result
SELECT DATE; /* March 20,2001 */ 01/03/20 (Default format)
SELECT EXTRACT(YEAR FROM DATE); 2001
SELECT EXTRACT(MONTH FROM DATE); 03
SELECT EXTRACT(DAY FROM DATE); 20
Date arithmetic may be applied to the date prior to the extraction. Added values always
represent days.
Query Result
SELECT EXTRACT(YEAR FROM DATE + 365); 2002
SELECT EXTRACT(MONTH FROM DATE + 30); 04
SELECT EXTRACT(DAY FROM DATE + 12); 01
Note:
Testing
Follow Hand
Query Result
SELECT TIME; /* 2:42 PM */ 14:42:32 (Default format)
SELECT EXTRACT(HOUR FROM TIME); 14
SELECT EXTRACT(MINUTE FROM TIME); 42
SELECT EXTRACT(SECOND FROM TIME); 32
Time arithmetic may be applied prior to the extraction. Added values always represent
seconds.
Query Result
SELECT EXTRACT(HOUR FROM TIME + 20); 14
SELECT EXTRACT(MINUTE FROM TIME + 20); 42
SELECT EXTRACT(SECOND FROM TIME + 20); 52
SELECT EXTRACT(SECOND FROM TIME + 30); Invalid Time