1/30/2002 staff development session 2002.03

28
1/30/2002 Staff Development Session 2002.03 1 DB2 Scalar Functions DB2 Scalar Functions Pam Odden

Upload: tess98

Post on 22-May-2015

196 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.031

DB2 Scalar FunctionsDB2 Scalar Functions

Pam Odden

Page 2: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.032

ObjectivesObjectives

Learn what a Scalar Function isWhere they can be used and what effect

scalar functions have on performanceWhat scalar functions are available in DB2

Versions 4 and 5

Page 3: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.033

DefinitionDefinition

Scalar Functions: functions that operate on a single value, and may be applied to a column or expression. The result is a transformed version of the column or expression.

These are in contrast to Column Functions, which are applied to a set of data.

Scalar: (accding to Webster’s Dictionary) adj. Designating a quantity that has magnitude but no direction in space, such as volume or temperature.

Page 4: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.034

Use of Scalar FunctionsUse of Scalar Functions

Can be used in SELECT clause and WHERE or HAVING clause

In SELECT clause there is usually no effect on performance

In WHERE or HAVING clause, performance can be slowed. Scalar functions do not use indexes.

Page 5: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.035

Functions that transform Character StringsFunctions that transform Character StringsSUBSTRSUBSTR

SUBSTR(string,start[,length]) Returns a specified portion of a character string String is a character or graphic string. Start is the starting position of the substring. It must be a number

between 1 and the length of the string. Length is the total length of the substring. If length is omitted, the

remainder of the string is returned.

select leavecode , substr(leavecode,1,2) FIRST_TWO_CHAR , substr(leavecode,3) THIRD_CHAR from ssasidb1.astu_student where ccsd_id = '357000'; ---------+---------+---------+---------+---------+LEAVECODE FIRST_TWO_CHAR THIRD_CHAR ---------+---------+---------+---------+---------+W1B W1 B

Page 6: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.036

SUBSTR (cont.)SUBSTR (cont.)Our book gives a slick example of using substr to get last name, first initial. It assumes that lastname

is a variable-length column (VARCHAR). In SASI, and probably most of our databases, names are fixed length, which will give the second result. See the example of the STRIP function (available in v. 5) for the desired result.

Select lastname || ', ' || substr(firstname,1,1) || '.' As LNAME_FI from ssasidb1.astu_student where schoolnum = '201'; ---------+---------+---------+---------+---------+---------+LNAME_FI ---------+---------+---------+---------+---------+---------+RAGNETTI, A. SMITH, B.

What I really got in SPUFI:---------+---------+---------+---------+---------+---------+LNAME_FI ---------+---------+---------+---------+---------+---------+RAGNETTI , A. SMITH , B.

Page 7: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.037

LENGTHLENGTH

LENGTH(expression) Returns the length of any column, including leading or trailing blanks

or zeros, as an integer. The null indicator byte is not counted. In strings, spaces are counted, but the length control field of a variable-

length string is not counted. Length returned is the number of bytes required to store the data type

2 for small integer3 for time4 for date and integer10 for timestamp

 

Page 8: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.038

LENGTH (cont.)LENGTH (cont.)The length function could be used to determine how many characters of a variable length

column are actually used. Say you are formatting a report and you’d like to truncate the teacher_comment column, varchar(256). You could find the longest comment:

  select max(length(teacher_comment)) as LONGEST_COMMENT from ssasidb1.astu_student; LONGEST_COMMENT 170 If you only have room for 150 characters, you could find out how many students have

comments that would be truncated: select count(*) NBR_LONG_COMMENTS from ssasidb1.astu_student where length(teacher_comment) > 150; NBR_LONG_COMMENTS 17

Page 9: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.039

STRIP (version 5)STRIP (version 5)

STRIP(expression,L/T,strip character) Removes leading and/or trailing characters from

expression L/T may be L, LEADING, B, BOTH, T,

TRAILING Strip character is the character to be removed If L/T is omitted, BOTH is assumed If strip character is omitted, space is assumed.

Page 10: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0310

Example using LENGTH and STRIPExample using LENGTH and STRIP

select firstname NAME

, length(strip(firstname)) LENGTH_STRIPPED from ssasipit.astu_student where permnum in (' 1947',' 53268',' 93239'); ---------+---------+---------+---------+---------+---------NAME LENGTH ---------+---------+---------+---------+---------+---------ELVIA 5 ALEXIS 6 ZACHARY 7  Select strip(lastname) || ', ' || substr(firstname,1,1) || '.' As LNAME_FI from ssasidb1.astu_student where schoolnum = '201';  ---------+---------+---------+---------+---------+---------+LNAME_FI ---------+---------+---------+---------+---------+---------+RAGNETTI, A. SMITH, B.

Page 11: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0311

HEXHEX

HEX(expression) Returns a hexidecimal character string representation of a

value with 2 hexadecimal digits per byte. If the expression is a graphic data item, the result will

contain 4 hexadecimal digits for each character. If the expression is null, the result will be null.

select hex(socsecnum) H_SOC

from ssasidb1.astu_student

where ccsd_id = ‘075160’;

 

H_SOC

---------+---------+--

F5F9F5F3F5F6F9F5F540

Page 12: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0312

Functions that transform Null ValuesFunctions that transform Null ValuesCOALESCE (or VALUE)COALESCE (or VALUE)

COALESCE(expression1,expression2[,expression3…])

Returns the first expression in the list that is not null.

The first expression is evaluated and, if not null, it is returned.

If the first expression is null, the next is evaluated, and returned if not null.

If all expressions in the list are null, null is returned.

Page 13: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0313

COALESCE (cont.)COALESCE (cont.)

Select custno, coalesce(‘H: ‘ || homephone, ‘W: ‘ || workphone, ‘NO PHONE NUMBER’) as PHONEfrom customer_tableorder by custno;  CUSTNO PHONE12345 H: 702-123-785912346 W: 702-875-789012347 H: 702-789-754312349 NO PHONE NUMBER

Page 14: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0314

NULLIF (version 5)NULLIF (version 5)

NULLIF(expression1,expression2) Returns null if the two expressions are equal, otherwise returns the first

expression. Expressions 1 and 2 must be compatible data types Not often used with COBOL, but useful with a programming language

that recognizes the null data type.

Select street || ‘ ‘ || nullif(po_box,’ ‘) || ’, ‘ || city || ‘, ‘ || state || ‘ ‘ || zip as ADDRESSfrom customer_table;ADDRESS126 Elm Street P.O. Box 123, Las Vegas, NV 890454576 Flamingo Road, Las Vegas, NV 89222

Page 15: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0315

Functions that transform DatesFunctions that transform Dates CHARCHAR

CHAR(expression[,format]) Returns a character representation of a date value Expression can be a date, time, or timestamp For date and time expressions, a format can be specified: ISO, USA,

EUR, JIS, LOCAL. DB2 generally defaults to the ISO format, unless your shop has

customized the general default

– ISO = International Standards Organization

– USA = IBM American Standard

– EUR = IBM European Standard

– JIS = Japanese Industrial Standard

– LOCAL = site specific (only works if coded for your site)

Page 16: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0316

CHAR (cont.)CHAR (cont.)Select current date as CURR_DATE , Char(current date,ISO) as DATE_ISO , char(current date,USA) as DATE_USA , char(current date,EUR) as DATE_EUR , char(current date,JIS) as DATE_JIS from ssasidb1.astu_student where permnum = ' 354678'; CURR_DATE DATE_ISO DATE_USA DATE_EUR DATE_JIS01/27/2002 2002-01-27 01/27/2002 27.01.2002 2002-01-27  Select current time as CURR_TIME , Char(current time,ISO) as DATE_ISO , char(current time,USA) as DATE_USA , char(current time,EUR) as DATE_EUR , char(current time,JIS) as DATE_JIS from ssasidb1.astu_student where permnum = ' 354678'; CURR_TIME TIME_ISO TIME_USA TIME_EUR TIME_JIS04:20 PM 16.20.34 04:20 PM 16.20.34 16:20:34

Page 17: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0317

DATE and TIMEDATE and TIMEDATE(expression)TIME(expression) Returns a date or time in default format For DATE, expression can be a date, timestamp, a string

representation of a date, or a positive number less than or equal to 3,652,059.

For TIME, expression can be a time, timestamp, or string representation of a time.

select current timestamp as TIMESTAMP, date(current timestamp) as DATE, rime(current timestamp) as TIMEfrom ssasidb1.astu_studentwhere permnum = ‘ 3456776’;  TIMESTAMP DATE TIME2002-01-28-09.06.46.940524 01/28/2002 09:06 AM

Page 18: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0318

TIMESTAMPTIMESTAMPTIMESTAMP(expression1[,expression2]) Returns a timestamp. When only 1 expression is used, it can be a timestamp or a string

representation of a timestamp. When 2 expressions are used, expression1 must be a date or string

representation of a date and expression2 must be a time or string representation of a time.

select current date as DATE, current time as TIME, timestamp(current date, current time) as TIMESTAMPfrom ssasidb1.astu_studentwhere permnum = ‘ 3456776’; DATE TIME TIMESTAMP01/28/2002 09:09 AM 2002-01-28-09.09.42.000000 (Notice when the timestamp is selected from the database, it

contains the microseconds. When it is created by the timestamp function, the microseconds are zero.)

Page 19: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0319

Components of Dates and TimesComponents of Dates and TimesYEAR(expr) Expr can be a date, dateMONTH(expr) duration, timestamp, or DAY(expr) timestamp duration.HOUR(expr) Expr can be a time, timeMINUTE(expr) duration, timestamp, orSECOND(expr) timestamp duration.MICROSECOND(expr)Each function returns the result as a binary fullword, PIC S9(9) COMP.

Select year(current date) as YEAR,month(current date) as MONTH,day(current date) as DAY,hour(current timestamp) as HOUR,minute(current timestamp) as MINUTE,second(current timestamp) as SECOND,microsecond(current timestamp) as MICROSECOND

from ssasidb1.astu_studentwhere permnum = ‘ 3456776’; YEAR MONTH DAY HOUR MINUTE SECOND MICROSECOND2002 1 28 9 26 26 444084

Page 20: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0320

DAYSDAYSDAYS(expression)Returns the number of days that have elapsed between

December 31, 0000 and the date of its argument Useful for calculating the number of days between two

dates Expression can be a date, timestamp, or string that DB2

can interpret as a date Result is a binary integer: PIC S9(9) COMP

Select days('2002-01-15') - days('2001-06-30') as DAYS_DIFFERENCE

From ssasidb1.astu_student Where permnum = ' 489675'; DAYS_DIFFERENCE 199

Page 21: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0321

DAYS (cont.)DAYS (cont.)

Select ccsd_id, enterdatedate, Days(enterdate) - days(‘08/30/2001’) as DAYS_LATE

From ssasidb1.astu_student Order by DAYS_LATE desc; CCSD_ID ENTERDATE DAYS_LATE313533 10/09/2001 40 331177 10/08/2001 39 301730 10/08/2001 39 419779 10/04/2001 35 192606 10/03/2001 34

Page 22: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0322

Functions that transform NumbersFunctions that transform NumbersDECIMALDECIMAL

DECIMAL(expression[,precision[,scale]]) Returns a decimal representation of the first argument. Precision specifies the total number of digits Scale specifies the number of digits to the right of the

decimal point. If precision or scale is omitted the result defaults to the

precision or scale of the first argument. A COBOL host variable is defined as COMP-3 and must

contain the correct number of digits on both sides of the decimal point. If it does not, an SQL error –406 will be returned.

Page 23: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0323

DECIMAL (cont.)DECIMAL (cont.)

(Note: the column credearned is defined in the ssasidb1 database as DECIMAL(7,3).) ---------+---------+---------+---------+---------+------ Select credearned, decimal(credearned,7,1) as DEC_7_1, decimal(credearned,9,5) as DEC_9_5 From ssasidb1.astu_student where credearned <> 0 ---------+---------+---------+---------+---------+------CREDEARNED DEC_7_1 DEC_9_5 ---------+---------+---------+---------+---------+------ .500 .5 .50000 4.500 4.5 4.50000 6.000 6.0 6.00000 7.500 7.5 7.50000 6.000 6.0 6.00000 5.500 5.5 5.50000 6.250 6.2 6.25000 .500 .5 .50000 1.166 1.1 1.16600

Page 24: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0324

INTEGERINTEGERINTEGER(expression) Returns the whole-number portion of expression, with any fractional

portion truncated. No rounding is performed. A COBOL host variable is defined as PIC S9(9) COMP.

Select credearned, integer(credearned) as INTEGER, integer(credearned + .5) as ROUNDED_INTEGER From ssasidb1.astu_student where credearned <> 0 ---------+---------+---------+---------+---------+------CREDEARNED INTEGER ROUNDED_INTEGER ---------+---------+---------+---------+---------+------ .500 0 1 4.500 4 5 6.000 6 6 16.700 16 17

Page 25: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0325

FLOATFLOAT

FLOAT(expression) Returns a double-precision floating point equivalent of expression. Expression must be a number. A COBOL host variable is be defined as COMP-2 with no

PICTURE clause.

Select credearned, float(credearned) as FLOAT From ssasidb1.astu_student where credearned <> 0 ---------+---------+---------+---------+---------+-----CREDEARNED FLOAT ---------+---------+---------+---------+---------+----- .500 +0.5000000000000000E+00 4.500 +0.4500000000000000E+01 6.000 +0.6000000000000000E+01 7.500 +0.7500000000000000E+01

Page 26: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0326

DIGITSDIGITSDIGITS(expression) Returns a character string of the digits in expression. The result does not include a sign or a decimal point. A COBOL host variable is defined as PIC X(n) where n is the length

of the data type of expression.

Select credearned, digits(credearned) as DIGITS From ssasidb1.astu_student where credearned <> 0 ---------+---------+---------+---------+---------+------CREDEARNED DIGITS ---------+---------+---------+---------+---------+------ .500 0000500 4.500 0004500 6.000 0006000 7.500 0007500 6.000 0006000

Page 27: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0327

VARGRAPHICVARGRAPHICVARGRAPHIC(expression) Returns a graphic string of the value of expression. Used with double-byte character strings. We will probably not use this in a COBOL program.

Select lastname,

vargraphic(lastname) as VARGRAPHIC

From ssasidb1.astu_student where schoolnum = '201';

---------+---------+---------+---------+---------+-----LASTNAME VARGRAPHIC ---------+---------+---------+---------+---------+-----ROBLES .âRâOâBâLâEâS MAY .âMâAâY MARCINKEVICH .âMâAâRâCâIâNâKâEâVâIâCâH

Page 28: 1/30/2002 Staff Development Session 2002.03

1/30/2002Staff Development Session 2002.0328

SummarySummary Scalar functions may be used to retrieve data in

the format needed and avoid some programming. Consider performance when using scalar

functions in a where clause, as no index will be used.

Not all of the available scalar functions are useful in COBOL programs, but most are a handy tool in SPUFI and other direct queries.