scalar functions and arithmetic · pdf file06.11.2017 · title: microsoft...

25
© Copyright IBM Corporation 2007 Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 4.0.3 Scalar Functions and Arithmetic 3.3.1

Upload: vodan

Post on 22-Mar-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

© Copyright IBM Corporation 2007Course materials may not be reproduced in whole or in part without the prior written permission of IBM. 4.0.3

Scalar Functions and Arithmetic

3.3.1

© Copyright IBM Corporation 2007

Unit Objectives

After completing this unit, you should be able to:

Use arithmetic in the SELECT and WHERE clauses

Use basic scalar functions such as COALESCE/VALUE, DECIMAL, SUBSTR

Use date and time scalar functions

Use the CONCAT operator

© Copyright IBM Corporation 2007

18270.0019180.0017250.0015340.0017750.0015900.0019950.00

1462.001534.001380.001227.001420.001272.001596.00

19732.0020714.0018630.0016567.0019170.0017172.0021546.00

SALARY COMMEMPNO000210000250000260000290000300000310000320

I need a list containingEMPNO, SALARY, COMM,

and SALARY + COMMfor employees whose salary

is less than $20000,ordered by employee number

SELECT EMPNO, SALARY, COMM,SALARY + COMM

FROM EMPLOYEEWHERE SALARY < 20000ORDER BY EMPNO

Selecting Calculated Values

© Copyright IBM Corporation 2007

SALARY COMM INCOMEEMPNO

18270.0019180.0017250.0015340.0017750.0015900.0019950.00

1462.001534.001380.001227.001420.001272.001596.00

19732.0020714.0018630.0016567.0019170.0017172.0021546.00

000210000250000260000290000300000310000320

SELECT EMPNO, SALARY, COMM, SALARY + COMM AS INCOMEFROM EMPLOYEEWHERE SALARY < 20000ORDER BY EMPNO

Naming Result Columns

© Copyright IBM Corporation 2007

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

I need a listing containingdepartment names and the

employee number of its manager,sorted by department name.

SELECT DEPTNAME, COALESCE (MGRNO, 'UNKNOWN')

AS MANAGERFROM DEPARTMENTORDER BY DEPTNAME

Substitution of NULL Values

© Copyright IBM Corporation 2007

SELECT EMPNO, SALARY, COMM,SALARY + COALESCE (COMM, 0)AS "TOTAL INCOME"

FROM EMPLOYEE

I need a list of the total income(salary and commission).

In the total, assume unknowncommissions to be zero.

EMPNO SALARY COMM TOTAL INCOME

000210 18270.00 1462.00 19732.00

000260 17250.00 - -

000290 15340.00 1227.00 16567.00

000300 17750.00 - -

... ... ... ...

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

FROM EMPLOYEE

EMPNO SALARY COMM TOTAL INCOME

000210 18270.00 1462.00 19732.00

000260 17250.00 - 17250.00

000290 15340.00 1227.00 16567.00

000300 17750.00 - 17750.00

... ... ... ...

Arithmetic with NULL Values

© Copyright IBM Corporation 2007

SALARYEMPNO18270.0019180.0017250.0015340.0017750.0015900.0019950.00

18955.12500019899.25000017896.87500015915.25000018415.62500016496.25000020698.125000

000210000250000260000290000300000310000320

SELECTFROMWHEREORDER BY

EMPNO, SALARY, SALARY * 1.0375EMPLOYEESALARY < 20000EMPNO

Calculated Values

© Copyright IBM Corporation 2007

SALARYEMPNO18270.0019180.0017250.0015340.0017750.0015900.0019950.00

18955.1219899.2517896.8715915.2518415.6216496.2520698.12

000210000250000260000290000300000310000320

SELECT

FROMWHEREORDER BY

EMPNO, SALARY,DECIMAL (SALARY * 1.0375, 8, 2) EMPLOYEESALARY < 20000EMPNO

Decimal Representation of a Value

© Copyright IBM Corporation 2007

SALARYEMPNO18270.0019180.0017250.0015340.0017750.0015900.0019950.00

18955.1319899.2517896.8815915.2518415.6316496.2520698.13

000210000250000260000290000300000310000320

SELECT

FROMWHEREORDER BY

EMPNO, SALARY,DECIMAL (SALARY * 1.0375 + 0.005, 8, 2) EMPLOYEESALARY < 20000EMPNO

Decimal Values - Truncation and Rounding

© Copyright IBM Corporation 2007

SELECTFROMWHEREORDER BY

EMPNO, COMM, SALARY, (COMM/SALARY) * 100EMPLOYEE(COMM/SALARY) * 100 > 8EMPNO

SALARYEMPNO COMM

2274.001462.002301.002030.00

28420.0018270.0028760.0025370.00

000140000210000240000330

8.0014008.0021008.0006008.001500

Condition on Calculated Values

© Copyright IBM Corporation 2007

Date Format

yyyy-mm-dd

mm/dd/yyyy

dd.mm.yyyy

yyyy-mm-dd

???

Length

8 bytes

8 bytes

8 bytes

8 bytes

???

Length

10 bytes

10 bytes

10 bytes

10 bytes

???

Format

ISO

USA

EUR

JIS

LOCAL

TIMESTAMP DATA:

Time Format

hh.mm.ss

hh:mm AM

hh:mm PM

hh.mm.ss

hh:mm:ss

???

yyyy-mm-dd-hh.mm.ss.nnnnnn 26 bytes

DataTypeDATETIMETIMESTAMP

Internal Formatyyyymmddhhmmssyyyymmddhhmmssnnnnnn

Internal Length4 bytes3 bytes10 bytes

Date and Time

DATE, TIME, TIMESTAMP data internally stored as packed decimal, without sign

Program uses an external format, that is,

© Copyright IBM Corporation 2007

CHAR controls the external format of date / time data

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

03:30 PM 15.30.00

CHAR Function

© Copyright IBM Corporation 2007

SELECTFROMWHEREORDER BY

EMPNO, LASTNAME, BIRTHDATEEMPLOYEEBIRTHDATE >= '1955-01-01'BIRTHDATE

BIRTHDATEEMPNO LASTNAME

000160000100

PIANKASPENCER

1955-04-121956-12-18

Comparison with Dates

© Copyright IBM Corporation 2007

DATE / TIME Arithmetic

Subtraction onlytime - 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

time + labeled duration time date + labeled duration datetimestamp + duration timestamp

© Copyright IBM Corporation 2007

QUINTANAGEYERGOUNOTLUCCHESSISETRIGHTMEHTA

790727.790727.781126.750607.740021.720901.

LASTNAME DIFFEREMPNO

000130000050000340000110000310000320

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 EMPLOYEEWHERE CURRENT_DATE - BIRTHDATE >= 720000ORDER BY DIFFER DESC

Subtraction of Dates

© Copyright IBM Corporation 2007

Date / Time Scalar Functions

DAY, MONTH, YEAR, HOUR, MINUTE, SECOND, MICROSECOND, DATE, TIME

Extract portions of a date, time, timestamp, or duration

DAYS - Converts a date to the number of days since 12/31/0000

DAYS(date_1) – DAYS(date_2) gives the number of days between date_1 and date_2

© Copyright IBM Corporation 2007

JOHNDOLORESJASONVINCENZOMAUDERAMLAL

790727.790727.781126.750607.740021.720901.

797978757472

2727267

211

DAYS

GEYERQUINTANAGOUNOTLUCCHESSISETRIGHTMEHTA

LASTNAME FIRSTNME AGE YEARS

77

11609

MONTHS

LASTNAME, FIRSTNME,CURRENT_DATE - BIRTHDATE AS AGE,YEAR(CURRENT_DATE - BIRTHDATE) AS YEARS,MONTH(CURRENT_DATE - BIRTHDATE) AS MONTHS,DAY(CURRENT_DATE - BIRTHDATE) AS DAYSEMPLOYEEYEAR(CURRENT_DATE - BIRTHDATE) >= 72AGE DESC, LASTNAME

SELECT

FROMWHEREORDER BY

Date Scalar Functions

© Copyright IBM Corporation 2007

PROJNO,DAYS(PRENDATE) - DAYS(PRSTDATE) AS DAYSPROJECTDAYS(PRENDATE) - DAYS(PRSTDATE) <= 300DAYS

SELECT

FROMWHEREORDER BY

DAYSPROJNO

PL2100MA2113

257289

DATE Arithmetic (1 of 2)

© Copyright IBM Corporation 2007

SELECT

FROMWHEREORDER BY

PROJNO, PRENDATE,PRENDATE + 2 MONTHS + 15 DAYSPROJECTPROJNO = 'AD3100'PROJNO

PRENDATEPROJNO

AD3100 1983-02-01 1983-04-16

DATE Arithmetic (2 of 2)

© Copyright IBM Corporation 2007

COURSINF

CC

FF

11

23

SS

QQ

LL

BA

AD D

SV

IA

CN

SC E

S Q L B A S I C SS Q L A D DV A N C E

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

Substring of Strings (1 of 2)

© Copyright IBM Corporation 2007

SELECT SUBSTR(PROJNO,1, 2) AS PROJ_CLASS,PROJNAME

FROM PROJECTWHERE PROJNO LIKE 'IF%'

PROJNAMEPROJ_CLASSIFIF

QUERY SERVICESUSER EDUCATION

Substring of Strings (2 of 2)

© Copyright IBM Corporation 2007

NAMEHAAS, CHRISTINELUCCHESSI, VINCENZOO'CONNELL, SEAN

SELECT

FROMWHEREORDER BY

LASTNAME CONCAT ', 'CONCAT FIRSTNME AS NAMEEMPLOYEEWORKDEPT = 'A00'NAME

Concatenation of Values

© Copyright IBM Corporation 2007

Checkpoint

1. True or False? If you use the DECIMAL scalar function to have two decimal positions instead of six, the result will be rounded.

2. If you subtract two dates, the format of the result will be: a. DD.MM.YYYY b. YYYY-MM-DD c. YYYYMMDD

3. Name several scalar functions.

© Copyright IBM Corporation 2007

Checkpoint Solutions

1. False

2. c

3. DECIMALSUBSTRCOALESCEVALUEYEARMONTHDAYDAYSROUND

© Copyright IBM Corporation 2007

Unit Summary

Having completed this unit, you should be able to:

Use arithmetic in the SELECT and WHERE clauses

Use basic scalar functions such as COALESCE/VALUE, DECIMAL, SUBSTR

Use date and time scalar functions

Use the CONCAT operator