Single-Row Functions<br />
Objectives<br />After completing this lesson, you should be able to do the following:<br />Describe various types of funct...
SQL Functions<br />Input<br />Output<br />arg 1<br />arg 2<br />Result<br />value<br />arg n<br />Function<br />Function p...
Two Types of SQL Functions<br />Functions<br />Multiple-row<br />functions<br />Single-row <br />functions<br />
Single-Row Functions<br />Manipulate data items<br />Accept arguments and return one value<br />Act on each row returned<b...
Single-Row Functions<br />Character<br />Number<br />General<br />Single-row <br />functions<br />Conversion<br />Date<br />
Character Functions<br />Character<br />functions<br />Character manipulation<br />functions<br />Case conversion <br />fu...
Function<br />Result<br />Case Conversion Functions<br />Convert case for character strings<br />sql course<br />SQL COURS...
SQL&gt; SELECTempno, ename, deptno<br />2  FROMemp<br />3  WHERE LOWER(ename) = &apos;blake&apos;;<br />    EMPNO ENAME   ...
Character Manipulation Functions<br />Manipulate character strings<br />Function<br />Result<br />GoodString<br />Str<br /...
Using the Character Manipulation Functions<br />SQL&gt; SELECT ename, CONCAT (ename, job), LENGTH(ename),<br />2INSTR(enam...
Number Functions<br />ROUND:		Rounds value to specified <br />decimal<br />ROUND(45.926, 2)						45.93<br />TRUNC:			Trunc...
Using the ROUND Function<br />SQL&gt; SELECT ROUND(45.923,2), ROUND(45.923,0),<br />2ROUND(45.923,-1)<br />3  FROM   DUAL;...
SQL&gt; SELECT TRUNC(45.923,2), TRUNC(45.923),<br />2TRUNC(45.923,-1)<br />3  FROM   DUAL;<br />TRUNC(45.923,2) TRUNC(45.9...
Using the MOD Function<br />Calculate the remainder of the ratio of salary to commission for all employees whose job title...
Working with Dates<br />Oracle stores dates in an internal numeric format: century, year, month, day, hours, minutes, seco...
Arithmetic with Dates<br />Add or subtract a number to or from a date for a resultant date value.<br />Subtract two dates ...
Using Arithmetic Operatorswith Dates<br />SQL&gt; SELECT ename, (SYSDATE-hiredate)/7 WEEKS<br />2  FROM   emp<br />3  WHER...
Date Functions<br />Function<br />Description<br />Number of monthsbetween two dates<br />MONTHS_BETWEEN<br />ADD_MONTHS<b...
<ul><li>MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')</li></ul>Using Date Functions<br />19.6774194<br /><ul><li>ADD_MONTHS ('1...
Using Date Functions<br /><ul><li>ROUND('25-JUL-95','MONTH')            01-AUG-95
ROUND('25-JUL-95','YEAR') 		 01-JAN-96
TRUNC('25-JUL-95','MONTH') 	 01-JUL-95
TRUNC('25-JUL-95','YEAR')		 01-JAN-95</li></li></ul><li>Conversion Functions<br />Datatype<br />conversion<br />Implicit d...
Implicit Datatype Conversion<br />For assignments, the Oracle can automatically convert the following:<br />From<br />To<b...
Implicit Datatype Conversion<br />For expression evaluation, the Oracle Server can automatically convert the following:<br...
Explicit Datatype Conversion<br />TO_NUMBER<br />TO_DATE<br />DATE<br />TO_CHAR<br />NUMBER<br />CHARACTER<br />TO_CHAR<br />
TO_CHAR Function with Dates<br />TO_CHAR(date, &apos;fmt&apos;)<br />The format model:<br />Must be enclosed in single quo...
Elements of Date Format Model<br />YYYY<br />Full year in numbers<br />YEAR<br />Year spelled out<br />MM<br />Two-digit v...
Elements of Date Format Model<br />HH24:MI:SS AM<br />15:45:32 PM<br />DD &quot;of&quot; MONTH<br />12 of OCTOBER<br />dds...
Add character strings by enclosing them in double quotation marks.
Number suffixes spell out numbers.</li></li></ul><li>Using TO_CHAR Function         with Dates<br />SQL&gt; SELECTename, <...
TO_CHAR Function with Numbers<br />TO_CHAR(number, &apos;fmt&apos;)<br />Use these formats with the TO_CHAR function to di...
Using TO_CHAR Function         with Numbers<br />SQL&gt; SELECTTO_CHAR(sal,&apos;$99,999&apos;) SALARY<br />2  FROMemp<br ...
TO_NUMBER and TO_DATE Functions <br />Convert a character string to a number format using the TO_NUMBER function<br />TO_N...
RR Date Format<br />Current Year<br />1995<br />1995<br />2001<br />2001<br />Specified Date<br />27-OCT-95<br />27-OCT-17...
NVL Function<br />Converts null to an actual value<br />Datatypes that can be used are date, character, and number.<br />D...
SQL&gt; SELECT ename, sal, comm, (sal*12)+NVL(comm,0)<br />2  FROM   emp;<br />ENAME            SAL      COMM (SAL*12)+NVL...
DECODE Function<br />Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement<br />DECODE(c...
Using the DECODE Function<br />SQL&gt; SELECT job, sal,<br />2         DECODE(job, &apos;ANALYST&apos;,  SAL*1.1,<br />3  ...
Using the DECODE Function<br />Display the applicable tax rate for each employee in department 30.<br />SQL&gt; SELECT ena...
Nesting Functions<br />Single-row functions can be nested to any level.<br />Nested functions are evaluated from deepest l...
Nesting Functions<br />SQL&gt; SELECTename,<br />2     NVL(TO_CHAR(mgr),&apos;No Manager&apos;)<br />3  FROMemp<br />4  WH...
Summary<br />Use functions to do the following:<br />Perform calculations on data<br />Modify individual data items<br />M...
Upcoming SlideShare
Loading in...5
×

Les03 Single Row Function

4,900

Published on

Published in: Travel, Business
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
4,900
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
316
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Les03 Single Row Function"

  1. 1. Single-Row Functions<br />
  2. 2. Objectives<br />After completing this lesson, you should be able to do the following:<br />Describe various types of functions available in SQL<br />Use character, number, and date functions in SELECT statements<br />Describe the use of conversion functions <br />
  3. 3. SQL Functions<br />Input<br />Output<br />arg 1<br />arg 2<br />Result<br />value<br />arg n<br />Function<br />Function performs action<br />
  4. 4. Two Types of SQL Functions<br />Functions<br />Multiple-row<br />functions<br />Single-row <br />functions<br />
  5. 5. Single-Row Functions<br />Manipulate data items<br />Accept arguments and return one value<br />Act on each row returned<br />Return one result per row<br />May modify the datatype<br />Can be nested<br />function_name (column|expression, [arg1, arg2,...])<br />
  6. 6. Single-Row Functions<br />Character<br />Number<br />General<br />Single-row <br />functions<br />Conversion<br />Date<br />
  7. 7. Character Functions<br />Character<br />functions<br />Character manipulation<br />functions<br />Case conversion <br />functions<br />LOWER<br />UPPER<br />INITCAP<br />CONCAT<br />SUBSTR<br />LENGTH<br />INSTR<br />LPAD<br />
  8. 8. Function<br />Result<br />Case Conversion Functions<br />Convert case for character strings<br />sql course<br />SQL COURSE<br />Sql Course<br />LOWER(&apos;SQL Course&apos;)<br />UPPER(&apos;SQL Course&apos;)<br />INITCAP(&apos;SQL Course&apos;)<br />
  9. 9. SQL&gt; SELECTempno, ename, deptno<br />2 FROMemp<br />3 WHERE LOWER(ename) = &apos;blake&apos;;<br /> EMPNO ENAME DEPTNO<br />--------- ---------- ---------<br />7698 BLAKE 30<br />Using Case Conversion Functions<br />Display the employee number, name, and department number for employee Blake.<br />SQL&gt; SELECTempno, ename, deptno <br />2 FROMemp<br />3 WHEREename = &apos;blake&apos;;<br />no rows selected<br />
  10. 10. Character Manipulation Functions<br />Manipulate character strings<br />Function<br />Result<br />GoodString<br />Str<br />6<br />3<br />******5000<br />CONCAT(&apos;Good&apos;, &apos;String&apos;)<br />SUBSTR(&apos;String&apos;,1,3)<br />LENGTH(&apos;String&apos;)<br />INSTR(&apos;String&apos;, &apos;r&apos;)<br />LPAD(sal,10,&apos;*&apos;)<br />
  11. 11. Using the Character Manipulation Functions<br />SQL&gt; SELECT ename, CONCAT (ename, job), LENGTH(ename),<br />2INSTR(ename, &apos;A&apos;)<br />3 FROM emp<br />4 WHERE<br />SUBSTR(job,1,5) = &apos;SALES&apos;;<br />ENAME CONCAT(ENAME,JOB) LENGTH(ENAME) INSTR(ENAME,&apos;A&apos;)<br />---------- ------------------- ------------- ----------------<br />MARTIN MARTINSALESMAN 62<br />ALLEN ALLENSALESMAN 51<br />TURNER TURNERSALESMAN 60<br />WARD WARDSALESMAN 42<br />
  12. 12. Number Functions<br />ROUND: Rounds value to specified <br />decimal<br />ROUND(45.926, 2) 45.93<br />TRUNC: Truncates value to specified decimal<br />TRUNC(45.926, 2) 45.92<br />MOD: Returns remainder of division<br />MOD(1600, 300) 100<br />
  13. 13. Using the ROUND Function<br />SQL&gt; SELECT ROUND(45.923,2), ROUND(45.923,0),<br />2ROUND(45.923,-1)<br />3 FROM DUAL;<br />ROUND(45.923,2) ROUND(45.923,0) ROUND(45.923,-1)<br />--------------- -------------- -----------------<br />45.924650<br />
  14. 14. SQL&gt; SELECT TRUNC(45.923,2), TRUNC(45.923),<br />2TRUNC(45.923,-1)<br />3 FROM DUAL;<br />TRUNC(45.923,2) TRUNC(45.923) TRUNC(45.923,-1)<br />--------------- ------------- ---------------<br />45.924540<br />Using the TRUNC Function<br />
  15. 15. Using the MOD Function<br />Calculate the remainder of the ratio of salary to commission for all employees whose job title is salesman.<br />SQL&gt; SELECTename, sal, comm, MOD(sal, comm)<br />2 FROMemp<br />3 WHEREjob = &apos;SALESMAN&apos;;<br />ENAME SAL COMM MOD(SAL,COMM)<br />---------- --------- --------- -------------<br />MARTIN 125014001250<br />ALLEN 1600300100<br />TURNER 150001500<br />WARD 1250500250<br />
  16. 16. Working with Dates<br />Oracle stores dates in an internal numeric format: century, year, month, day, hours, minutes, seconds.<br />The default date format is DD-MON-YY.<br />SYSDATE is a function returning date and time.<br />DUAL is a dummy table used to view SYSDATE.<br />
  17. 17. Arithmetic with Dates<br />Add or subtract a number to or from a date for a resultant date value.<br />Subtract two dates to find the numberof days between those dates.<br />Add hours to a date by dividing the number of hours by 24.<br />
  18. 18. Using Arithmetic Operatorswith Dates<br />SQL&gt; SELECT ename, (SYSDATE-hiredate)/7 WEEKS<br />2 FROM emp<br />3 WHERE deptno = 10;<br />ENAME WEEKS<br />---------- ---------<br />KING 830.93709<br />CLARK 853.93709<br />MILLER 821.36566<br />
  19. 19. Date Functions<br />Function<br />Description<br />Number of monthsbetween two dates<br />MONTHS_BETWEEN<br />ADD_MONTHS<br />Add calendar months to date<br />NEXT_DAY<br />Next day of the date specified<br />LAST_DAY<br />Last day of the month<br />ROUND<br />Round date <br />TRUNC <br />Truncate date<br />
  20. 20. <ul><li>MONTHS_BETWEEN ('01-SEP-95','11-JAN-94')</li></ul>Using Date Functions<br />19.6774194<br /><ul><li>ADD_MONTHS ('11-JAN-94',6)</li></ul>&apos;11-JUL-94&apos;<br /><ul><li>NEXT_DAY ('01-SEP-95','FRIDAY') </li></ul>&apos;08-SEP-95&apos;<br /><ul><li>LAST_DAY('01-SEP-95')</li></ul>&apos;30-SEP-95&apos;<br />
  21. 21. Using Date Functions<br /><ul><li>ROUND('25-JUL-95','MONTH') 01-AUG-95
  22. 22. ROUND('25-JUL-95','YEAR') 01-JAN-96
  23. 23. TRUNC('25-JUL-95','MONTH') 01-JUL-95
  24. 24. TRUNC('25-JUL-95','YEAR') 01-JAN-95</li></li></ul><li>Conversion Functions<br />Datatype<br />conversion<br />Implicit datatype<br />conversion<br />Explicit datatype<br />conversion<br />
  25. 25. Implicit Datatype Conversion<br />For assignments, the Oracle can automatically convert the following:<br />From<br />To<br />VARCHAR2 or CHAR<br />NUMBER<br />VARCHAR2 or CHAR<br />DATE<br />NUMBER<br />VARCHAR2<br />DATE<br />VARCHAR2<br />
  26. 26. Implicit Datatype Conversion<br />For expression evaluation, the Oracle Server can automatically convert the following:<br />From<br />To<br />VARCHAR2 or CHAR<br />NUMBER<br />VARCHAR2 or CHAR<br />DATE<br />
  27. 27. Explicit Datatype Conversion<br />TO_NUMBER<br />TO_DATE<br />DATE<br />TO_CHAR<br />NUMBER<br />CHARACTER<br />TO_CHAR<br />
  28. 28. TO_CHAR Function with Dates<br />TO_CHAR(date, &apos;fmt&apos;)<br />The format model:<br />Must be enclosed in single quotation marks and is case sensitive<br />Can include any valid date format element<br />Has an fm element to remove padded blanks or suppress leading zeros<br />Is separated from the date value by a comma<br />
  29. 29. Elements of Date Format Model<br />YYYY<br />Full year in numbers<br />YEAR<br />Year spelled out<br />MM<br />Two-digit value for month<br />MONTH<br />Full name of the month<br />Three-letter abbreviation of the day of the week<br />DY<br />DAY<br />Full name of the day<br />
  30. 30. Elements of Date Format Model<br />HH24:MI:SS AM<br />15:45:32 PM<br />DD &quot;of&quot; MONTH<br />12 of OCTOBER<br />ddspth<br />fourteenth<br /><ul><li>Time elements format the time portion of the date.
  31. 31. Add character strings by enclosing them in double quotation marks.
  32. 32. Number suffixes spell out numbers.</li></li></ul><li>Using TO_CHAR Function with Dates<br />SQL&gt; SELECTename, <br />2TO_CHAR(hiredate, &apos;fmDD Month YYYY&apos;) HIREDATE<br />3 FROM emp;<br />ENAME HIREDATE<br />---------- -----------------<br />KING 17 November 1981<br />BLAKE 1 May 1981<br />CLARK 9 June 1981<br />JONES 2 April 1981<br />MARTIN 28 September 1981<br />ALLEN 20 February 1981<br />...<br />14 rows selected.<br />
  33. 33. TO_CHAR Function with Numbers<br />TO_CHAR(number, &apos;fmt&apos;)<br />Use these formats with the TO_CHAR function to display a number value as a character:<br />9<br />Represents a number<br />0<br />Forces a zero to be displayed<br />$<br />Places a floating dollar sign<br />L<br />Uses the floating local currency symbol<br />.<br />Prints a decimal point<br />,<br />Prints a thousand indicator<br />
  34. 34. Using TO_CHAR Function with Numbers<br />SQL&gt; SELECTTO_CHAR(sal,&apos;$99,999&apos;) SALARY<br />2 FROMemp<br />3 WHEREename = &apos;SCOTT&apos;;<br />SALARY<br />--------<br />$3,000<br />
  35. 35. TO_NUMBER and TO_DATE Functions <br />Convert a character string to a number format using the TO_NUMBER function<br />TO_NUMBER(char[, &apos;fmt&apos;])<br /><ul><li>Convert a character string to a date format using the TO_DATE function</li></ul>TO_DATE(char[, &apos;fmt&apos;])<br />
  36. 36. RR Date Format<br />Current Year<br />1995<br />1995<br />2001<br />2001<br />Specified Date<br />27-OCT-95<br />27-OCT-17<br />27-OCT-17<br />27-OCT-95<br />RR Format<br />1995<br />2017<br />2017<br />1995<br />YY Format<br />1995<br />1917<br />2017<br />2095<br />If the specified two-digit year is:<br />0–49<br />50–99<br />If two digits of the current year are:<br />The return date is in the century before the current one<br />The return date is in the current century<br />0–49<br />The return date is in the century after the current one<br />The return date is in the current century<br />50–99<br />
  37. 37. NVL Function<br />Converts null to an actual value<br />Datatypes that can be used are date, character, and number.<br />Datatypes must match <br />NVL(comm,0)<br />NVL(hiredate,&apos;01-JAN-97&apos;)<br />NVL(job,&apos;No Job Yet&apos;)<br />
  38. 38. SQL&gt; SELECT ename, sal, comm, (sal*12)+NVL(comm,0)<br />2 FROM emp;<br />ENAME SAL COMM (SAL*12)+NVL(COMM,0)<br />---------- --------- --------- --------------------<br />KING 500060000<br />BLAKE 285034200<br />CLARK 245029400<br />JONES 297535700<br />MARTIN 1250140016400<br />ALLEN 160030019500<br />...<br />14 rows selected.<br />Using the NVL Function<br />
  39. 39. DECODE Function<br />Facilitates conditional inquiries by doing the work of a CASE or IF-THEN-ELSE statement<br />DECODE(col/expression, search1, result1<br />[, search2, result2,...,]<br />[, default])<br />
  40. 40. Using the DECODE Function<br />SQL&gt; SELECT job, sal,<br />2 DECODE(job, &apos;ANALYST&apos;, SAL*1.1,<br />3 &apos;CLERK&apos;, SAL*1.15,<br />4 &apos;MANAGER&apos;, SAL*1.20,<br />5 SAL)<br />6 REVISED_SALARY<br />7 FROM emp;<br />JOB SAL REVISED_SALARY<br />--------- --------- --------------<br />PRESIDENT 50005000<br />MANAGER 28503420<br />MANAGER 24502940<br />...<br />14 rows selected.<br />
  41. 41. Using the DECODE Function<br />Display the applicable tax rate for each employee in department 30.<br />SQL&gt; SELECT ename, sal,<br />2 DECODE(TRUNC(sal/1000, 0),<br />30, 0.00,<br />41, 0.09,<br />52, 0.20,<br />63, 0.30,<br />74, 0.40,<br />85, 0.42,<br />96, 0.44,<br />100.45) TAX_RATE<br />11 FROM emp<br />12 WHERE deptno = 30;<br />
  42. 42. Nesting Functions<br />Single-row functions can be nested to any level.<br />Nested functions are evaluated from deepest level to the least-deep level.<br />F3(F2(F1(col,arg1),arg2),arg3)<br />Step 1 = Result 1<br />Step 2 = Result 2<br />Step 3 = Result 3<br />
  43. 43. Nesting Functions<br />SQL&gt; SELECTename,<br />2 NVL(TO_CHAR(mgr),&apos;No Manager&apos;)<br />3 FROMemp<br />4 WHEREmgr IS NULL;<br />ENAME NVL(TO_CHAR(MGR),&apos;NOMANAGER&apos;)<br />---------- -----------------------------<br />KING No Manager<br />
  44. 44. Summary<br />Use functions to do the following:<br />Perform calculations on data<br />Modify individual data items<br />Manipulate output for groups of rows<br />Alter date formats for display<br />Convert column datatypes<br />
  45. 45. Practice Overview<br />Creating queries that require the use of numeric, character, and date functions<br />Using concatenation with functions<br />Writing case-insensitive queries to test the usefulness of character functions<br />Performing calculations of years and months of service for an employee<br />Determining the review date for an employee<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×