0% found this document useful (0 votes)
9 views3 pages

Handouts - 14 (Date Fun)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Handouts - 14 (Date Fun)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Quality Thought ETL Testing Daily Handouts

Note: Follow Handouts & Material & Examples which are discussing in classes to get good knowledge on ETL Testing
Session-14

Topics Covered in session:


 Date Functions
 Date Arithmetic
 Ad_Months
 Extract

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;

Scenario:How would you find a person's age (in rounded years)?


SELECT (DATE-birthdate)/365
FROM employee WHERE last_name = 'Stein';

Scenario: Find employees with more than 10 years of service.


SELECT last_name,hire_date FROM employee
WHERE (DATE-hire_date)/365 > 10;

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

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

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.

Comparison of ADD_MONTHS and simple DATE arithmetic


Because of the variable number of days in a month, ADD_MONTH provides a much higher
degree of accuracy when projecting dates based on month increments.

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;

Using the EXTRACT Function


Extracting From Current Date
The EXTRACT function allows for easy extraction of year, month and day from any DATE
data type. The following examples demonstrate its usage.

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

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]
Quality Thought ETL Testing Daily Handouts

Extracting From Current Time


The EXTRACT function may also be applied against the current time. It permits extraction
of hours, minutes and seconds. The following examples demonstrate its usage.

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

QUALITY THOUGHT * www.facebook.com/qthought * www.qualitythought.in


PH NO: 9963486280, 040-40025423 EMAIL ID: [email protected]

You might also like