Oracle: Functions

1,768 views
1,620 views

Published on

Oracle: Functions

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,768
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Oracle: Functions

  1. 1. 1<br />Using ORACLE®<br />Using functions<br />(single row and aggregate functions)<br />And the ‘GROUP BY ‘ clause<br />
  2. 2. 2<br />FUNCTIONS<br />We have many functions in Oracle that can be used to perform a myriad of tasks.<br />There are two types of functions viz:<br />Functions take input ,process it and provide the output.<br />Single row functions operate only on one row (1 input and 1 output)<br />Aggregate functions can operate on a range of rows <br />( multiple inputs and one output)<br />
  3. 3. 3<br />FUNCTIONS<br />Single row functions can be divided into different parts as:<br />Character<br />Single row functions<br />Number<br />General<br />Conversion<br />Date<br />
  4. 4. 4<br />CHARACTER FUNCTIONS<br />Case manipulation functions<br />Character manipulation functions<br />
  5. 5. 5<br />CHARACTER FUNCTIONS<br />SELECTprod_ID, INITCAP (prod_name) , CONCAT ('Rs ',prod_cost) AS COST <br />FROM product_master<br />WHERE prod_name LIKE( LOWER ('TEAK%'));<br />SELECTprod_ID, UPPER (prod_name) , CONCAT ('Rs ',prod_cost) AS COST <br />FROM product_master<br />WHERE prod_name LIKE ( CONCAT ( '%', SUBSTR ( 'teak_sofa', 6, 4 )));<br />Equivalent to LIKE ( ‘%sofa’);<br />
  6. 6. 6<br />NUMBER FUNCTIONS<br />SELECT ROUND (3.14126,4), TRUNC (3.14126,4) , MOD ( 22, 7 ) <br />FROM DUAL ;<br />(DUAL is a dummy table in Oracle used to perform calculations.)<br />SELECTprod_ID , prod_name , ROUND( MOD (prod_stock,prod_order),0) AS REORDER_LVL<br />FROM product_master<br />ORDER BY REORDER_LVL;<br />(Here we use the alias name to order the output)<br />
  7. 7. 7<br />DATE FUNCTIONS<br />We can use either MONTH or YEAR as parameters in the ROUND and TRUNC functions.<br />
  8. 8. 8<br />DATE MANIPULATIONS<br />Apart from the date functions we can also perform arithmetic operations on dates as:.<br />Adding or subtracting a number to or from a date to get a resultant date.<br />Subtracting two dates to find the number of days between those dates.<br />Adding hours to a date by dividing the number of hours by 24.<br />SELECT SYSDATEASTODAY,<br /> (SYSDATE+1) AS NEXT_DAY,<br /> (SYSDATE-1) AS PREV_DAY<br />FROM DUAL;<br />
  9. 9. 9<br />CONVERSION FUNCTIONS<br />Here in the TO_NUMBER function we are converting the string ‘2000’ to a number 2000.<br />In TO_DATE we have to specify the format of the output date and <br />In TO_CHAR we have to specify the format of input date to be converted to a string<br />
  10. 10. 10<br />GENERAL FUNCTIONS<br />SELECT prod_ID, prod_name, NVL (prod_order,0), NVL2 (prod_sales,'YES','NO'), <br />NULLIF (prod_stock,(prod_order+prod_sales)) AS NULLIF<br />FROM product_master;<br />
  11. 11. 11<br />AGGREGATE FUNCTIONS<br />Aggregate functions take multiple rows as input and give one output. The SYNTAX is:<br />SELECT column1,column2…….aggregate_function( column)…<br />FROMtable_name<br />WHERE (expression/condition);<br />
  12. 12. 12<br />AGGREGATE FUNCTIONS<br />SELECT COUNT (prod_ID) AS COUNT, MIN (prod_cost) AS MINIMUM, <br />MAX (prod_sales) AS MAXIMUM, AVG (prod_cost) AS AVERAGE, <br />SUM (prod_stock) AS SUM, ROUND( STDDEV (prod_sales),3) AS STD_DEVIAYION,<br />ROUND( VARIANCE (prod_stock),3) AS VARIANCE<br />FROM product_master;<br />(We can also use ‘*’ in COUNT and ‘DISTINCT’ in AVG )<br />
  13. 13. 13<br />GROUP BY CLAUSE<br />When we wish to divide the table into multiple groups based on some criterion we use the ‘GROUP BY’ clause.<br />It is mandatory that while using a group by clause we include the column used in the group must not be used in a aggregate function in the SELECT statement.<br />SYNTAX:<br />SELECT column1,column2…….aggregate_function( column)…<br />FROMtable_name<br />GROUP BY column<br />WHERE (expression/condition);<br />It is mandatory to use the group by clause when displaying any column along with a aggregratefunction.We can also use multiple columns in the group by clause.<br />Use the WHERE clause to furthur filter results.<br />
  14. 14. THANK YOU<br />14<br />THANK YOU FOR VIEWING THIS PRESENTATION<br />FOR MORE PRESENTATIONS AND VIDEOS ON ORACLE AND DATAMINING ,<br />please visit: <br />www.dataminingtools.net<br />

×