0% found this document useful (0 votes)
35 views

Calculated Values: by Neil A. Basabe

This document contains examples and explanations of how to perform calculations and comparisons using date and time data types in SQL. It shows how to calculate new values based on existing columns, compare dates, perform date/time arithmetic like subtraction and addition, and work with special date/time registers. Examples demonstrate finding records where the commission percentage is over a certain amount, comparing birthdates to a given date, calculating the difference between dates in years and months, and adding durations like months and days to a date.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Calculated Values: by Neil A. Basabe

This document contains examples and explanations of how to perform calculations and comparisons using date and time data types in SQL. It shows how to calculate new values based on existing columns, compare dates, perform date/time arithmetic like subtraction and addition, and work with special date/time registers. Examples demonstrate finding records where the commission percentage is over a certain amount, comparing birthdates to a given date, calculating the difference between dates in years and months, and adding durations like months and days to a date.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

CALCULATED VALUES

By
Neil A. Basabe
Calculated Values
SELECT EMPNO, SALARY,
SALARY * 1.0375 AS NEWSALARY
FROM EMPLOYEE
WHERE SALARY<20000
ORDER BY EMPNO;

09/30/2020 Prepared by: SB Satorre 2


Condition on Calculated Values
SELECT EMPNO, COMM, SALARY,
(COMM/SALARY)*100 AS ADJCOMM
FROM EMPLOYEE
WHERE (COMM/SALARY)*100>8
ORDER BY EMPNO;

09/30/2020 Prepared by: SB Satorre 3


Date and Time
Data Type Internal Format Internal Length
DATE yyyymmdd 4 bytes
TIME hhmmss 3 bytes
TIMESTAMP yyyymmddhhmmssnnnnnn 10 bytes

09/30/2020 Prepared by: SB Satorre 4


Comparison with Dates
• Columns defined with a DATE or TIME data
type can be compared with other DATE or
TIME columns or with a valid date or time.
• Date or time constants (character string
representations of a valid date or time) must
be placed in single quotes. All legal formats
can be used.

09/30/2020 Prepared by: SB Satorre 5


SELECT EMPNO, LASTNAME, BIRTHDATE
FROM EMPLOYEE
WHERE BIRTHDATE>='1955-01-01'
ORDER BY BIRTHDATE;

09/30/2020 Prepared by: SB Satorre 6


DATE/TIME Arithmetic
• Subtraction
  Only
 time – time = time duration (decimal (6,0))
 date – date = date duration (decimal(8,0))
 timestamp – timestamp = timestamp duration
(decimal(20,6))
• Labeled Durations: YEARS, MONTHS, DAYS, HOURS,
MINUTES, SECONDS, MICROSECONDS
 t labeled duration = time
 d labeled duration = date
 t labeled duration = timestamp
09/30/2020 Prepared by: SB Satorre 7
Subtraction of Dates
• CURRENT_DATE, CURRENT_TIME and
CURRENT_TIMESTAMP are special registers
provided by DB2 containing the system date,
system time, and system timestamp.

09/30/2020 Prepared by: SB Satorre 8


Example – Subtraction of Dates
SELECT EMPNO, LASTNAME,
CURRENT_DATE-BIRTHDATE AS DIFFER
FROM EMPLOYEE
WHERE CURRENT_DATE-BIRTHDATE>450000
ORDER BY DIFFER DESC;

Note: 450000 means 45 years, 00 months and 00 days

09/30/2020 Prepared by: SB Satorre 9


Note: 580,703 means 58 years, 07 months, and 03 days.

09/30/2020 Prepared by: SB Satorre 10


DATE Arithmetic - 1
SELECT PROJNO,
DAYS(PRENDATE)-DAYS(PRSTDATE) AS DAYS
FROM PROJECT
WHERE DAYS(PRENDATE)-DAYS(PRSTDATE) <= 300
ORDER BY DAYS;

09/30/2020 Prepared by: SB Satorre 11


DATE Arithmetic - 2
SELECT PROJNO, PRENDATE,
PRENDATE + 2 MONTHS + 15 DAYS AS
"NEW END DATE"
FROM PROJECT
WHERE PROJNO = 'AD3100'
ORDER BY PROJNO;

09/30/2020 Prepared by: SB Satorre 12


Laboratory Exercises

You might also like