sql functions - numeric and date speaker notes contain additional information!

30
SQL functions - numeric and date Speaker notes contain additional information!

Upload: letitia-goodman

Post on 26-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: SQL functions - numeric and date Speaker notes contain additional information!

SQL functions - numeric and date

Speaker notes contain additional information!

Page 2: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 2000

SQL> SELECT ROUND(salary/12,2) 2 FROM first_pay;

ROUND(SALARY/12,2)------------------ 3750 3333.33 2083.33 3500 4166.67 4000

This command is rounding the calculation of salary divided by 12. It is rounding to 2 decimal places.

number of decimal places

Page 3: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

SQL> SELECT ROUND(salary/7,2), TRUNC(salary/7,2) 2 FROM first_pay;

ROUND(SALARY/7,2) TRUNC(SALARY/7,2)----------------- ----------------- 6428.57 6428.57 5714.29 5714.28 3571.43 3571.42 6000 6000 7142.86 7142.85 6857.14 6857.14

SQL> SELECT salary/7 2 FROM first_pay;

SALARY/7---------6428.5714 Both rounding and truncating are .575714.2857 Rounding goes to .29, truncating leaves at .283571.4286 Rounding goes to .43, truncating leaves at .42 6000 No rounding or truncating7142.8571 Rounding goes to .86, truncating leaves at .856857.1429 Both rounding and truncating are .14

Page 4: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

SQL> SELECT salary, MOD(salary,3) 2 FROM first_pay;

SALARY MOD(SALARY,3)--------- ------------- 45000 0 40000 1 25000 1 42000 0 50000 2 48000 0

SQL> SELECT salary, MOD(salary, 12) 2 FROM first_pay;

SALARY MOD(SALARY,12)--------- -------------- 45000 0 40000 4 25000 4 42000 0 50000 8 48000 0

SQL> SELECT salary, MOD(salary,7) 2 FROM first_pay;

SALARY MOD(SALARY,7)--------- ------------- 45000 4 40000 2 25000 3 42000 0 50000 6 48000 1

MOD returns the remainder that results from the divide. There are three different examples. Note that the format is MOD(column,number you are dividing by)

number dividing by

Page 5: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

SQL> SELECT salary/7, CEIL(salary/7), FLOOR(salary/7) 2 FROM first_pay;

SALARY/7 CEIL(SALARY/7) FLOOR(SALARY/7)--------- -------------- ---------------6428.5714 6429 64285714.2857 5715 57143571.4286 3572 3571 6000 6000 60007142.8571 7143 71426857.1429 6858 6857

SQL> SELECT salary/7, CEIL(salary/7) 2 FROM first_pay 3 WHERE CEIL(salary/7) = ROUND(salary/7,0);

SALARY/7 CEIL(SALARY/7)--------- --------------6428.5714 6429 6000 60007142.8571 7143

This shows CEIL and FLOOR in calculations and CEIL in a WHERE clause comparison.

Page 6: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

SQL> SELECT bonus, POWER(bonus,2), POWER(bonus/100,3), POWER(bonus/100,4) 2 FROM first_pay;

BONUS POWER(BONUS,2) POWER(BONUS/100,3) POWER(BONUS/100,4)--------- -------------- ------------------ ------------------ 1000 1000000 1000 10000 1500 2250000 3375 50625 500 250000 125 625 2000 4000000 8000 160000 2000 4000000 8000 160000 2000 4000000 8000 160000

To the second power To the third powerTo the forth power

Format: POWER(column or formula, power)

Page 7: SQL functions - numeric and date Speaker notes contain additional information!

Numeric functionsNumeric functions

SQL> SELECT bonus, SIGN(MOD(bonus,bonus)), SIGN(bonus/-1), SIGN(bonus) 2 FROM first_pay;

BONUS SIGN(MOD(BONUS,BONUS)) SIGN(BONUS/-1) SIGN(BONUS)--------- ---------------------- -------------- ----------- 1000 0 -1 1 1500 0 -1 1 500 0 -1 1 2000 0 -1 1 2000 0 -1 1 2000 0 -1 1

SIGN(MOD(bonus,bonus) means bonus is divided by bonus and the sign of the remainder is checked to see if it is 0, >0, <0

SIGN returns a 0 if the number being evaluated is a 0, a 1 if the number being evaluated is >0, and a -1 if the number being evaluated is <0.

Page 8: SQL functions - numeric and date Speaker notes contain additional information!

Date functionDate function

SQL> SELECT sysdate 2 FROM dual;

SYSDATE---------02-JUN-00

Sysdate gets the date from the system. Notice the default presentation.

SQL> SELECT sysdate 2 FROM sys.dual;

SYSDATE---------02-JUN-00

Page 9: SQL functions - numeric and date Speaker notes contain additional information!

Date functionsDate functions

SQL> SELECT sysdate, ROUND(sysdate), TRUNC(sysdate) 2 FROM dual;

SYSDATE ROUND(SYS TRUNC(SYS--------- --------- ---------03-JUN-00 04-JUN-00 03-JUN-00

SQL> SELECT sysdate, ROUND(sysdate), TRUNC(sysdate) 2 FROM dual;

SYSDATE ROUND(SYS TRUNC(SYS--------- --------- ---------03-JUN-00 03-JUN-00 03-JUN-00 I did the first date at 7:10 in the AM.

NOTE: All three dates are the same.I then went into the system clock andchanged it to PM. When I ran the command again, ROUND went to the next day.

Page 10: SQL functions - numeric and date Speaker notes contain additional information!

Date functionsDate functions

SQL> SELECT startdate, ROUND(startdate), TRUNC(startdate) 2 FROM first_pay;

STARTDATE ROUND(STA TRUNC(STA--------- --------- ---------15-JAN-97 15-JAN-97 15-JAN-9725-SEP-92 25-SEP-92 25-SEP-9205-FEB-00 05-FEB-00 05-FEB-0003-JUL-97 03-JUL-97 03-JUL-9730-OCT-92 30-OCT-92 30-OCT-9218-AUG-94 18-AUG-94 18-AUG-94

The default on ROUND and TRUNC is day. MONTH and YEAR can be specified.

SQL> SELECT ROUND(startdate,'MONTH'),TRUNC(startdate,'MONTH') 2 FROM first_pay;

ROUND(STA TRUNC(STA--------- ---------01-JAN-97 01-JAN-9701-OCT-92 01-SEP-9201-FEB-00 01-FEB-0001-JUL-97 01-JUL-9701-NOV-92 01-OCT-9201-SEP-94 01-AUG-94

When ROUND is used with MONTH, dates with days such as 25-SEP are rounded to OCT and 30-OCT are rounded to NOV. Dates with 15-JAN and 05-FEB are simply rounded to the first day of the month.

NOTE: The day is 01 when rounded or truncated.

Page 11: SQL functions - numeric and date Speaker notes contain additional information!

Date functionsDate functions

SQL> SELECT startdate, ROUND(startdate,'YEAR'), TRUNC(startdate,'YEAR') 2 FROM first_pay;

STARTDATE ROUND(STA TRUNC(STA--------- --------- ---------15-JAN-97 01-JAN-97 01-JAN-9725-SEP-92 01-JAN-93 01-JAN-9205-FEB-00 01-JAN-00 01-JAN-0003-JUL-97 01-JAN-98 01-JAN-9730-OCT-92 01-JAN-93 01-JAN-9218-AUG-94 01-JAN-95 01-JAN-94

When the ROUND and TRUNC are done to a year 01-JAN are used for day and month. For months like JUL,AUG, SEP and OCT ROUND shows the next year.

For dates like JAN and FEB ROUND shows the same year.

Page 12: SQL functions - numeric and date Speaker notes contain additional information!

Date functionsDate functions

SQL> SELECT name, startdate, MONTHS_BETWEEN(startdate, sysdate) 2 FROM first_pay;

NAME STARTDATE MONTHS_BETWEEN(STARTDATE,SYSDATE)-------------------- --------- ---------------------------------Linda Costa 15-JAN-97 1159.3767John Davidson 25-SEP-92 1107.6992Susan Ash 05-FEB-00 -3.945912Stephen York 03-JUL-97 1165Richard Jones 30-OCT-92 1108.8605Joanne Brown 18-AUG-94 1130.4734

THE SYSDATE being used is 03-JUN-00. Note that 05-FEB-00 is -3.9… months back. Looking at 15-JAN-97 you can see that the number is going forward. The assumption is being made that the first two digits are 20, and we are looking forward and taking the difference between 15-JAN-2000 and 03-JUN-2000. This is because this table was created in 2000 and therefore the date was stored with the 20.

Page 13: SQL functions - numeric and date Speaker notes contain additional information!

SQL> SELECT datefst, MONTHS_BETWEEN(sysdate, datefst) FROM donor;

DATEFST MONTHS_BETWEEN(SYSDATE,DATEFST)--------- -------------------------------03-JUL-98 2324-MAY-97 36.35312503-JAN-98 2904-MAR-92 98.99828604-MAR-92 98.99828604-APR-98 25.998286

Date functionsDate functions

Since this table was created in 1999, the data is calculated with dates in 19__. NOTE: the whole year is actually stored with the date.

There are 23 months between 03-JUL-98 and 03-JUN-00 (the sysdate).

Page 14: SQL functions - numeric and date Speaker notes contain additional information!

SQL> SELECT datefst, TO_CHAR(datefst, 'MM/DD/YYYY') FROM donor;

DATEFST TO_CHAR(DATEFST,'MM/DD/YYYY')--------- -------------------------------------------------------03-JUL-98 07/03/199824-MAY-97 05/24/199703-JAN-98 01/03/199804-MAR-92 03/04/199204-MAR-92 03/04/199204-APR-98 04/04/1998

SQL> SELECT startdate, TO_CHAR(startdate,'MM/DD/YYYY') FROM first_pay;

STARTDATE TO_CHAR(STARTDATE,'MM/DD/YYYY')--------- -------------------------------------------------------------15-JAN-97 01/15/209725-SEP-92 09/25/209205-FEB-00 02/05/200003-JUL-97 07/03/209730-OCT-92 10/30/209218-AUG-94 08/18/2094

Date functionsDate functions

When I show the date in a format that shows the four digit year, I can see that the first_pay database which was created in 2000 has all the years in 2000. The donor database which was created in 1999 has all the years in the 1990s.

TO_CHAR will be covered later in this presentation. It converts dates or numbers to character fields using a specified format. In this case, the format is MM/DD/YYYY to show the four character year.

Page 15: SQL functions - numeric and date Speaker notes contain additional information!

Date functionDate function

SQL> SELECT name, startdate, ADD_MONTHS(startdate,6), ADD_MONTHS(startdate,-6) 2 FROM first_pay;

NAME STARTDATE ADD_MONTH ADD_MONTH-------------------- --------- --------- ---------Linda Costa 15-JAN-97 15-JUL-97 15-JUL-96John Davidson 25-SEP-92 25-MAR-93 25-MAR-92Susan Ash 05-FEB-00 05-AUG-00 05-AUG-99Stephen York 03-JUL-97 03-JAN-98 03-JAN-97Richard Jones 30-OCT-92 30-APR-93 30-APR-92Joanne Brown 18-AUG-94 18-FEB-95 18-FEB-94

SQL> SELECT name, startdate, ADD_MONTHS(startdate,2), ADD_MONTHS(startdate,-2) 2 FROM first_pay;

NAME STARTDATE ADD_MONTH ADD_MONTH-------------------- --------- --------- ---------Linda Costa 15-JAN-97 15-MAR-97 15-NOV-96John Davidson 25-SEP-92 25-NOV-92 25-JUL-92Susan Ash 05-FEB-00 05-APR-00 05-DEC-99Stephen York 03-JUL-97 03-SEP-97 03-MAY-97Richard Jones 30-OCT-92 30-DEC-92 30-AUG-92Joanne Brown 18-AUG-94 18-OCT-94 18-JUN-94

In one example went ahead and then back 6 months, in the other example I used 2 months. Notice that the difference between 2000 and 1999 are handled correctly.

Page 16: SQL functions - numeric and date Speaker notes contain additional information!

Date functionDate function

SQL> SELECT startdate, NEXT_DAY(startdate,'MONDAY') 2 FROM first_pay;

STARTDATE NEXT_DAY(--------- ---------15-JAN-97 21-JAN-9725-SEP-92 29-SEP-9205-FEB-00 07-FEB-0003-JUL-97 08-JUL-9730-OCT-92 03-NOV-9218-AUG-94 23-AUG-94

Looking at the dates in first_pay, I am determining the next time a MONDAY will occur starting from that date.

For example, 05-FEB-00 is a SATURDAY, so the next time a MONDAY will occur is 07-FEB-00.

SQL> SELECT sysdate, NEXT_DAY(sysdate,'TUESDAY') 2 FROM dual;

SYSDATE NEXT_DAY(--------- ---------03-JUN-00 06-JUN-00

Today is a SATURDAY, so in fact the next time a TUESDAY occurs will be 3 days from now on 06-JUN-00.

Page 17: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT name, startdate, TO_CHAR(startdate,'MM/YYYY') 2 FROM first_pay;

NAME STARTDATE-------------------- ---------TO_CHAR(STARTDATE,'MM/YYYY')---------------------------------------------------------------------------Linda Costa 15-JAN-9701/2097

John Davidson 25-SEP-9209/2092

Susan Ash 05-FEB-0002/2000

Stephen York 03-JUL-9707/2097

Richard Jones 30-OCT-9210/2092

Joanne Brown 18-AUG-9408/2094

The TO_CHAR startdate has a lot of padding and so it wraps - even on the Oracle screen. The default column size is 80 characters. This can be resized with the column command to be covered later.

Note: The month is displayed in number format because of the MM which returns 1-12 and the year is displayed as a 4 digit year because of the YYYY format.

See notes for additional information.

Page 18: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT name, startdate, TO_CHAR(startdate,'fmMM/YYYY') 2 FROM first_pay;

NAME STARTDATE-------------------- ---------TO_CHAR(STARTDATE,'FMMM/YYYY')-------------------------------------------------------------Linda Costa 15-JAN-971/2097

John Davidson 25-SEP-929/2092

Susan Ash 05-FEB-002/2000

Stephen York 03-JUL-977/2097

Richard Jones 30-OCT-9210/2092

Joanne Brown 18-AUG-948/2094

The fm element can be used to leading zeros or padded blanks. In this case it will remove the leading 0 from the month.

Page 19: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT sysdate, TO_CHAR(sysdate,'Q') 2 FROM dual;

SYSDATE TO_CHAR(SYSDATE,'Q')--------- ---------------------------------------------------------04-JUN-00 2

Q returns the quarter. June is in the second quarter.

SQL> SELECT sysdate, TO_CHAR(sysdate,'fmDDMONTHYYYY') 2 FROM dual;

SYSDATE TO_CHAR(SYSDATE,'FMDDMONTHYYYY')--------- --------------------------------04-JUN-00 4JUNE2000

SQL> SELECT sysdate, TO_CHAR(sysdate, 'fmDD MONTH YYYY') 2 FROM dual;

SYSDATE TO_CHAR(SYSDATE,'FMDDMONTHYYYY')--------- --------------------------------04-JUN-00 4 JUNE 2000

fmDD returns the day with leading zeros removed

MONTH returns the month in words

YYYY returns the year in 4 digits

NOTE the difference between the format without spaces between elements and with spaces between elements.

Page 20: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT TO_CHAR(sysdate,'fmDD "of" MONTH') 2 FROM dual;

TO_CHAR(SYSDATE,'FMDD"OF"MONTH')----------------------------------------------4 of JUNE

Double quotes are used to enclose character strings that are embedded in the format.

SQL> SELECT TO_CHAR(sysdate, '"DAY" DDSP "OF" MONTH') 2 FROM dual;

TO_CHAR(SYSDATE,'"DAY"DDSP"OF"MONTH')-------------------------------------------------------DAY FOUR OF JUNE

DDSP means spelled out DD.

SQL> SELECT TO_CHAR(startdate, 'fmDDTH "of" MONTH') 2 FROM first_pay;

TO_CHAR(STARTDATE,'FMDDTH"OF"MONTH')------------------------------------------------------15TH of JANUARY25TH of SEPTEMBER5TH of FEBRUARY3RD of JULY30TH of OCTOBER18TH of AUGUST

DDTH adds TH or other appropriate clause to date.

Page 21: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT TO_CHAR(sysdate,'HH24:MI:SS:AM') 2 FROM dual;

TO_CHAR(SYSDATE,'HH24:MI:SS:AM')--------------------------------------------14:23:48:PM

SQL> SELECT TO_CHAR(sysdate, 'HH12:MI:SS:AM') 2 FROM dual;

TO_CHAR(SYSDATE,'HH12:MI:SS:AM')----------------------------------------------02:24:56:PM

HH24 is 24 hour clock and HH12 is 12 hour clock. HH hour of day.

Note that AM changes to PM when appropriate. PM could also have been used.

SQL> SELECT TO_CHAR(sysdate, 'HH:MI:SS') 2 FROM dual;

TO_CHAR(SYSDATE,'HH:MI:SS')-----------------------------------------02:31:17

Page 22: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> ALTER SESSION SET NLS_DATE_FORMAT='DD-MON-RR';

Session altered.

SQL> INSERT INTO first_pay 2 VALUES('8888','Paula Adams','IN','12-DEC-98',45000,2000);

1 row created.

SQL> SELECT name, TO_CHAR(startdate,'DD-MON-YYYY') 2 FROM first_pay;

NAME TO_CHAR(STARTDATE,'DD-MON-YYYY')-------------------- -------------------------------------------Linda Costa 15-JAN-2097John Davidson 25-SEP-2092Susan Ash 05-FEB-2000Stephen York 03-JUL-2097Richard Jones 30-OCT-2092Joanne Brown 18-AUG-2094Donald Brown 05-NOV-2099Paula Adams 12-DEC-1998

8 rows selected.

The session is altered as explained in the speaker notes. Therefore, Paula Adams is given a year of 1998 instead of 2098.

Page 23: SQL functions - numeric and date Speaker notes contain additional information!

SQL> UPDATE first_pay 2 SET startdate = '05-NOV-99' 3 WHERE name = 'Donald Brown';

1 row updated.

SQL> SELECT name, TO_CHAR(startdate,'DD-MON-YYYY') 2 FROM first_pay;

NAME TO_CHAR(STARTDATE,'DD-MON-YYYY')-------------------- ----------------------------------Linda Costa 15-JAN-2097John Davidson 25-SEP-2092Susan Ash 05-FEB-2000Stephen York 03-JUL-2097Richard Jones 30-OCT-2092Joanne Brown 18-AUG-2094Donald Brown 05-NOV-1999Paula Adams 12-DEC-1998

Conversion functionsConversion functions

I am still in the session where RR is established so when I change the date for Donald Brown, it becomes 1999 instead of the previous 2099.

Page 24: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT TO_CHAR(sysdate, 'fmDDSPTH "day of" MONTH", "YYYY "at" HH12 AM') 2 FROM dual;

TO_CHAR(SYSDATE,'FMDDSPTH"DAYOF"MONTH","YYYY"AT"HH12AM')---------------------------------------------------------------------------FOURTH day of JUNE, 2000 at 4 PM

Note: MONTH”, “YYYY means that there is no space after month and only the space embedded in the literal before year. Notice also that HH gives the hour only. The actual time is 4:07 but I did not ask for minutes.

Page 25: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functionsSQL> DESC first_pay; Name Null? Type ------------------------------- -------- ---- PAY_ID VARCHAR2(4) NAME VARCHAR2(20) JOBCODE CHAR(2) STARTDATE DATE SALARY NUMBER(9,2) BONUS NUMBER(5)

SQL> SELECT pay_id, name, TO_CHAR(salary,'$99,999.99'), TO_CHAR(bonus,'$99,999.99') 2 FROM first_pay;

PAY_ NAME TO_CHAR(SAL TO_CHAR(BON---- -------------------- ----------- -----------1111 Linda Costa $45,000.00 $1,000.002222 John Davidson $40,000.00 $1,500.003333 Susan Ash $25,000.00 $500.004444 Stephen York $42,000.00 $2,000.005555 Richard Jones $50,000.00 $2,000.006666 Joanne Brown $48,000.00 $2,000.007777 Donald Brown $45,000.00 $2,000.008888 Paula Adams $45,000.00 $2,000.00

Note that BONUS has no decimal places, put in formatting I want it to display with decimal places. This can be done through formatting.

This is a floating $ sign that floats up to the first significant digit. Note that with 500 the comma gets suppressed because there is no significant digit in the thousands position.

Page 26: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT name, TO_CHAR(salary,'99,999.99'), TO_CHAR(bonus, '9,999') 2 FROM first_pay;

NAME TO_CHAR(SA TO_CHA-------------------- ---------- ------Linda Costa 45,000.00 1,000John Davidson 40,000.00 1,500Susan Ash 25,000.00 500Stephen York 42,000.00 2,000Richard Jones 50,000.00 2,000Joanne Brown 48,000.00 2,000Donald Brown 45,000.00 2,000Paula Adams 45,000.00 2,000

SQL> SELECT name, TO_CHAR(salary, '99999.99'), TO_CHAR(bonus, '0,009') 2 FROM first_pay;

NAME TO_CHAR(S TO_CHA-------------------- --------- ------Linda Costa 45000.00 1,000John Davidson 40000.00 1,500Susan Ash 25000.00 0,500Stephen York 42000.00 2,000Richard Jones 50000.00 2,000Joanne Brown 48000.00 2,000Donald Brown 45000.00 2,000Paula Adams 45000.00 2,000

Editing without the dollar sign but with the comma inserted. Leading zeros do not display.

There is no comma specified for salary and bonus has leading zeros being displayed.

Page 27: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT TO_CHAR(-567,'999MI'), TO_CHAR(-0123, '9,999PR') 2 FROM dual;

TO_C TO_CHAR---- -------567- <123>

MI prints the minus sign at the end and PR encloses the negative data in <…>. Note that if the data is not negative there is no special indication.

SQL> SELECT TO_CHAR(1243, '9999MI'), TO_CHAR(345,'999PR') 2 FROM dual;

TO_CH TO_CH----- -----1243 345

Page 28: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT pay_id, TO_NUMBER(pay_id) 2 FROM first_pay;

PAY_ TO_NUMBER(PAY_ID)---- -----------------1111 11112222 22223333 33334444 44445555 55556666 66667777 77778888 8888

SQL> SELECT * 2 FROM first_pay 3 WHERE TO_NUMBER(pay_id) = 2222;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------2222 John Davidson IN 25-SEP-92 40000 1500

SQL> DESC first_pay; Name Null? Type ------------------------------- -------- ---- PAY_ID VARCHAR2(4) NAME VARCHAR2(20) JOBCODE CHAR(2) STARTDATE DATE SALARY NUMBER(9,2) BONUS NUMBER(5)

In the first example, I converted pay_id to a number - you can see it aligned to the right of the field.

In the second example, I compared the converted pay_id to a number.

Page 29: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT * FROM first_pay;

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------1111 Linda Costa CI 15-JAN-97 45000 10002222 John Davidson IN 25-SEP-92 40000 15003333 Susan Ash AP 05-FEB-00 25000 5004444 Stephen York CM 03-JUL-97 42000 20005555 Richard Jones CI 30-OCT-92 50000 20006666 Joanne Brown IN 18-AUG-94 48000 20007777 Donald Brown CI 05-NOV-99 45000 20008888 Paula Adams IN 12-DEC-98 45000 2000

8 rows selected.

SQL> SELECT * 2 FROM first_pay 3 WHERE startdate > TO_DATE('15-JUN-97');

PAY_ NAME JO STARTDATE SALARY BONUS---- -------------------- -- --------- --------- ---------4444 Stephen York CM 03-JUL-97 42000 2000

Since the system date is in 2000, the assumption is that I am looking for dates greater than June 15, 2097. Only one meets this criteria. The last two dates have 19 as their first two digits.

Page 30: SQL functions - numeric and date Speaker notes contain additional information!

Conversion functionsConversion functions

SQL> SELECT name, yrgoal, NVL(yrgoal,0) 2 FROM donor;

NAME YRGOAL NVL(YRGOAL,0)--------------- --------- -------------Stephen Daniels 500 500Jennifer Ames 400 400Carl Hersey 0Susan Ash 100 100Nancy Taylor 50 50Robert Brooks 50 50

SQL> SELECT name, yrgoal, NVL(yrgoal, -1000) 2 FROM donor;

NAME YRGOAL NVL(YRGOAL,-1000)--------------- --------- -----------------Stephen Daniels 500 500Jennifer Ames 400 400Carl Hersey -1000Susan Ash 100 100Nancy Taylor 50 50Robert Brooks 50 50

In one example, I displayed null and in the other example I displayed -1000 in the column where the null value occurred.