0% found this document useful (0 votes)
98 views24 pages

Scalar Functions and Arithmetic

modul database

Uploaded by

Bagus
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)
98 views24 pages

Scalar Functions and Arithmetic

modul database

Uploaded by

Bagus
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/ 24

Scalar Functions and Arithmetic

© Copyright IBM Corporation 2010, 2013


Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 7.0
Unit objectives
After completing this unit, you should be able to:
• Use arithmetic in the SELECT and WHERE clauses
• Specify basic scalar functions such as COALESCE/VALUE,
DECIMAL, SUBSTR
• Use date and time scalar functions
• Use the CONCAT operator

© Copyright IBM Corporation 2010, 2013


Selecting calculated values

I need a list containing


EMPNO, SALARY, COMM,
and SALARY + COMM
for employees whose salary
is less than $20000, SELECT EMPNO, SALARY, COMM,
ordered by employee number SALARY + COMM
FROM EMPLOYEE
WHERE SALARY < 20000
ORDER BY EMPNO;

EMPNO SALARY COMM


000210 18270.00 1462.00 19732.00
000250 19180.00 1534.00 20714.00
000260 17250.00 1380.00 18630.00
000290 15340.00 1227.00 16567.00
000300 17750.00 1420.00 19170.00
000310 15900.00 1272.00 17172.00
000320 19950.00 1596.00 21546.00
© Copyright IBM Corporation 2010, 2013
Naming result set columns

SELECT EMPNO, SALARY, COMM, SALARY + COMM AS INCOME


FROM EMPLOYEE
WHERE SALARY < 20000
ORDER BY EMPNO;

EMPNO SALARY COMM INCOME

000210 18270.00 1462.00 19732.00


000250 19180.00 1534.00 20714.00
000260 17250.00 1380.00 18630.00
000290 15340.00 1227.00 16567.00
000300 17750.00 1420.00 19170.00
000310 15900.00 1272.00 17172.00
000320 19950.00 1596.00 21546.00
© Copyright IBM Corporation 2010, 2013
Substitution of NULL values

I need a listing containing department


names and the employee number of its
manager, sorted by department name.

SELECT DEPTNAME
, COALESCE (MGRNO, 'UNKNOWN')
AS MANAGER
FROM DEPARTMENT
ORDER BY DEPTNAME;

DEPTNAME MANAGER
ADMINISTRATION SYSTEMS 000070
DEVELOPMENT CENTER UNKNOWN
INFORMATION CENTER 000030
MANUFACTURING SYSTEMS 000060
OPERATIONS 000090
PLANNING 000020
SOFTWARE SUPPORT 000100
SPIFFY COMPUTER SERVICE DIV. 000010
SUPPORT SERVICES 000050

© Copyright IBM Corporation 2010, 2013


Arithmetic with NULL values

I need a list of the total income


(salary and commission).
In the total, assume unknown
commissions to be zero.

SELECT EMPNO, SALARY, COMM,


SELECT EMPNO, SALARY, COMM,
SALARY +
SALARY + COMM COALESCE(COMM, 0)
AS TOTAL_INCOME AS TOTAL_INCOME
FROM EMPLOYEE; FROM EMPLOYEE;

EMPNO SALARY COMM TOTAL INCOME EMPNO SALARY COMM TOTAL INCOME
000210 18270.00 1462.00 19732.00 000210 18270.00 1462.00 19732.00
000260 17250.00 - - 000260 17250.00 - 17250.00
000290 15340.00 1227.00 16567.00 000290 15340.00 1227.00 16567.00
000300 17750.00 - - 000300 17750.00 - 17750.00
... ... ... ...
... ... ... ...

© Copyright IBM Corporation 2010, 2013


Calculated values

SELECT EMPNO, SALARY, SALARY * 1.0375


FROM EMPLOYEE
WHERE SALARY < 20000
ORDER BY EMPNO;

EMPNO SALARY
000210 18270.00 18955.125000
000250 19180.00 19899.250000
000260 17250.00 17896.875000
000290 15340.00 15915.250000
000300 17750.00 18415.625000
000310 15900.00 16496.250000
000320 19950.00 20698.125000
© Copyright IBM Corporation 2010, 2013
Rounding and Truncation

SELECT EMPNO, SALARY, DECIMAL(ROUND(SALARY * 1.0375, 2), 8, 2)


FROM EMPLOYEE
WHERE SALARY < 20000
ORDER BY EMPNO;

EMPNO SALARY
000210 18270.00 18955.13
000250 19180.00 19899.25
000260 17250.00 17896.88
000290 15340.00 15915.25
000300 17750.00 18415.63
000310 15900.00 16496.25
000320 19950.00 20698.13

© Copyright IBM Corporation 2010, 2013


Condition on calculated values

SELECT EMPNO, COMM, SALARY, (COMM/SALARY) * 100


FROM EMPLOYEE
WHERE (COMM/SALARY) * 100 > 8
ORDER BY EMPNO;

EMPNO COMM SALARY


000140 2274.00 28420.00 8.001400
000210 1462.00 18270.00 8.002100
000240 2301.00 28760.00 8.000600
000330 2030.00 25370.00 8.001500

© Copyright IBM Corporation 2010, 2013


Upper and lower case

000090 Henderson
000150 ADAMSON (Column values
000230 JEFFERsON in the DB2 table)
000260 JOHNSOn

SELECT EMPNO,LASTNAME
FROM EMPLOYEE
WHERE UPPER(LASTNAME) like '%SON%‘ ;

SELECT EMPNO,LASTNAME
FROM EMPLOYEE
WHERE LOWER(LASTNAME) like '%son%‘ ;

000090 Henderson
(Result set as 000150 ADAMSON
returned to the user) 000230 JEFFERsON
000260 JOHNSOn
© Copyright IBM Corporation 2010, 2013
Substring of strings (1 of 2)

COURSINF

C F1 2 SQL B AS I CS
C F1 3 SQL A DV A NCED

SELECT SUBSTR(COURSINF, 6, 15) ...

SQL B AS I CS
SQL A DV A NCED

© Copyright IBM Corporation 2010, 2013


Concatenation of values

SELECT LASTNAME || ', ' || FIRSTNME AS NAME


FROM EMPLOYEE
WHERE WORKDEPT = 'A00'
ORDER BY NAME;

NAME
HAAS, CHRISTINE
LUCCHESSI, VINCENZO
O'CONNELL, SEAN

© Copyright IBM Corporation 2010, 2013


Order of precedence in predicate processing

1. Evaluation of contents within parentheses, beginning with


the innermost set of parentheses
2. All Boolean operator AND’s are processed before the OR’s
3. Multiplication and division are performed before addition
and subtraction
4. Process from Left to Right
? * or /
+ or - ? ?
? OR
(…..) AND
?
© Copyright IBM Corporation 2010, 2013
DB2 DATE, TIME and TIMESTAMP
• DATE, TIME, and TIMESTAMP data are internally stored
in packed decimal format
Data Type Internal Format Internal Length
DATE yyyymmdd 4 bytes
TIME hhmmss 3 bytes
TIMESTAMP yyyymmddhhmmssnnnnnn 10 bytes

• The result set displays to the user in these formats:


Format Time Format Length Date Format Length
ISO hh.mm.ss 8 bytes yyyy-mm-dd 10 bytes
USA hh:mm AM 8 bytes mm/dd/yyyy 10 bytes
hh:mm PM 8 bytes
EUR hh.mm.ss dd.mm.yyyy 10 bytes
JIS hh:mm:ss 8 bytes yyyy-mm-dd 10 bytes
LOCAL ??? ??? ??? ???
TIMESTAMP DATA: yyyy-mm-dd-hh.mm.ss.nnnnnn 26 bytes
© Copyright IBM Corporation 2010, 2013
DB2: Different date formats

• CHAR controls the external format of date / time data

SELECT CHAR (TIMECOL, USA), CHAR (TIMECOL, ISO)...

03:30 PM 15.30.00

© Copyright IBM Corporation 2010, 2013


DB2 DATE comparison

SELECT EMPNO, LASTNAME, BIRTHDATE


FROM EMPLOYEE
WHERE BIRTHDATE >= '1955-01-21'
ORDER BY BIRTHDATE;

EMPNO LASTNAME BIRTHDATE


000160 PIANKA 1955-04-12
000100 SPENCER 1956-12-18

© Copyright IBM Corporation 2010, 2013


DB2 Special Registers

CURRENT DATE
CURRENT TIME
CURRENT TIMESTAMP

CURRENT_DATE
CURRENT_TIME
CURRENT_TIMESTAMP

Example, in ISO format:


CURRENT DATE CURRENT TIME CURRENT TIMESTAMP
------------ ------------ -----------------
2011-07-29 09.45.22 2011-07-29-09.45.22.030000

© Copyright IBM Corporation 2010, 2013


DB2 DATE / TIME scalar functions
• DAY, MONTH, YEAR, HOUR, MINUTE, SECOND,
MICROSECOND, DATE, TIME
Extract portions of a date, time, timestamp, or duration

• DAYS - Converts any date to the number of days since


January 1st, 1 AD
DAYS(date_1) – DAYS(date_2) gives the number of days
between date_1 and date_2

SELECT PROJNO, PRENDATE, DAY(PRENDATE) AS DAY,


MONTH(PRENDATE) AS MONTH,
YEAR(PRENDATE) AS YEAR
FROM PROJECT
WHERE PROJNO = 'PL2100‘;

PROJNO PRENDATE DAY MONTH YEAR


------ -------- --- ----- ----
PL2100 09/15/2011 15 09 2011
© Copyright IBM Corporation 2010, 2013
DB2 subtraction of dates (1 of 2)

I need a listing containing the ages


of all employees 72 years old or
more, sorted by age in descending
sequence.

SELECT EMPNO, LASTNAME, CURRENT_DATE -


BIRTHDATE AS DIFFER
FROM EMPLOYEE
WHERE CURRENT_DATE - BIRTHDATE >= 720000
ORDER BY DIFFER DESC;

EMPNO LASTNAME DIFFER


000130 QUINTANA 790727.
000050 GEYER 790727.
000340 GOUNOT 781126.
000110 LUCCHESSI 750607.
000310 SETRIGHT 740021.
000320 MEHTA 720901.
© Copyright IBM Corporation 2010, 2013
DB2 subtraction of dates (2 of 2)

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

PROJNO DAYS
PL2100 257
MA2113 289

© Copyright IBM Corporation 2010, 2013


DB2 DATE arithmetic and labeled durations

SELECT PROJNO, PRENDATE,


PRENDATE + 2 MONTHS + 15 DAYS
FROM PROJECT
WHERE PROJNO = 'AD3100'
ORDER BY PROJNO;

PROJNO PRENDATE

AD3100 1983-02-01 1983-04-16

© Copyright IBM Corporation 2010, 2013


Checkpoint

1. If you use SELECT ROUND(SALARY,-2) on a row in the


EMPLOYEE table, how far are you rounding the salary?
2. How would you find the duration of an activity in days, using
DB2?
3. Name as many of the scalar functions as you can
remember.

© Copyright IBM Corporation 2010, 2013


Checkpoint solutions

1. To the nearest 100.

2. SELECT DAYS(EMSTDATE) - DAYS(EMENDATE)

3. DECIMAL, ROUND, UPPER, LOWER, SUBSTR, CHAR,


YEAR, MONTH, DAY, DAYS, HOUR, MINUTE, SECOND,
COALESCE, CONCAT

© Copyright IBM Corporation 2010, 2013


Unit summary
Having completed this unit, you should be able to:
• Use arithmetic in the SELECT and WHERE clauses
• Specify basic scalar functions such as COALESCE/VALUE,
DECIMAL, SUBSTR
• Use date and time scalar functions
• Use the CONCAT operator

© Copyright IBM Corporation 2010, 2013

You might also like