SQL Functions

325 views
202 views

Published on

Using and Creating SQL Functions with Ammar Hassan Brohi.


String Functions
Numeric Functions
String / Number Conversion Functions
Group Functions
Date and Time Functions
Date Conversion Functions

Published in: Data & Analytics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
325
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
25
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

SQL Functions

  1. 1. SQL Function FreakDeveloper
  2. 2. Function? • Stored piece of program that Manipulates submitted data and return some value. FreakDeveloper
  3. 3. Where use SQL Functions? • SELECT Statement SELECT Ucase(name) FROM students • WHERE Statement SELECT * FROM students WHERE substr(class,1,1) FreakDeveloper
  4. 4. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  5. 5. String Functions • LOWER( string ) • All letters are changed to lowercase. • AMMAR ammar FreakDeveloper LOWER ( s )
  6. 6. String Functions • UPPER( string ) • All letters are changed to uppercase • ammar AMMAR FreakDeveloper LOWER ( s )
  7. 7. String Functions • INSTR( string, find, pos, occ) • Finds string within string. • INSTR(‘Ammar’, ‘ar’)  4 • INSTR(‘Ammar’, ‘a’,1,2)  4 FreakDeveloper
  8. 8. String Functions • LENGTH( string) • Tells length of string • LENGTH(‘Ammar’)  5 FreakDeveloper
  9. 9. String Functions • LPAD( string, length, pad-string) • Adds string to its left. • LPAD(‘Ammar’,10)  ‘ Ammar’ • LPAD(‘Ammar’,10, ‘-’)  ‘-----Ammar’ FreakDeveloper
  10. 10. String Functions • RPAD( string, length, pad-string) • Adds string to its right. • RPAD(‘Ammar’,10)  ‘Ammar ’ • RPAD(‘Ammar’,10, ‘-’)  ‘Ammar-----’ FreakDeveloper
  11. 11. String Functions • LTRIM( string, characters) • Trims out string from left. • LTRIM(‘ Ammar’)  ‘Ammar’ • LTRIM(‘Ammar’, ‘m’)  ‘Aar’ FreakDeveloper
  12. 12. String Functions • RTRIM( string, characters) • Trims out string from right. • RTRIM(‘Ammar ’)  ‘Ammar’ • RTRIM(‘Ammar’, ‘r’)  ‘Amm’ FreakDeveloper
  13. 13. String Functions • SUBSTR( string, pos, number) • Takes out string from string. • SUBSTR(‘Ammar’, 3)  ‘mar’ • SUBTR(‘Ammar’, 3, 2)  ‘ma’ FreakDeveloper
  14. 14. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  15. 15. Numeric Functions • ABS ( value) • Returns absolute value • ABS(-13)  13 FreakDeveloper
  16. 16. Numeric Functions • MOD( value, divisor) • Returns remainder of value divided by divisor. • MOD(4,2)  0 FreakDeveloper
  17. 17. Numeric Functions • POWER( value, exp) • Raises power of value to its exp • POWER(2,2)  4 FreakDeveloper
  18. 18. Numeric Functions • ROUND ( value, decimal) • Rounds value to decimal places. • ROUND (20.6, 0)  21 FreakDeveloper
  19. 19. Numeric Functions • CEIL ( value) • Rounds value upwards. • CEIL(20.6, 0)  21 • CEIL(-20.6, 0)  20 FreakDeveloper
  20. 20. Numeric Functions • FLOOR ( value) • Rounds value downwards. • FLOOR(20.6, 0)  20 • FLOOR(-20.6, 0)  21 FreakDeveloper
  21. 21. Numeric Functions • SQRT ( value) • Returns Square root of value. • SQRT(36)  6 • SQRT(81)  9 FreakDeveloper
  22. 22. Numeric Functions • SQRT ( value) • Returns Square root of value. • SQRT(36)  6 • SQRT(81)  9 FreakDeveloper
  23. 23. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  24. 24. Numeric Functions • NANVL ( value, format) • If value NaN, it will show replace_with • NANVL(‘Ammar’, 15)  15 • NANVL(10, 15)  10 FreakDeveloper
  25. 25. Numeric Functions • TO_CHAR ( value, format) • Converts value into string using format. • TO_CHAR(100.11, $999.9)  ‘$100.1’ • TO_CHAR(100.11, 999)  ‘100’ FreakDeveloper
  26. 26. Numeric Functions • TO_NUMBER ( string, format) • Converts string into number using format. • TO_NUMBER(‘100.11’, 999.9)  100.1 FreakDeveloper
  27. 27. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  28. 28. Group Functions • AVG ( column) • Finds average of column • AVG (rent)  $1300 FreakDeveloper
  29. 29. Group Functions • Count ( column) • Returns number of rows. • Count (*) from orders  140 • Count (customer_id) from orders • WHERE customer_id=4  9 FreakDeveloper
  30. 30. Group Functions • MAX ( column) • Returns maximum value in column. • MAX(marks) from students 99 • SELECT student_name from students • WHERE marks = MAX(marks) FreakDeveloper
  31. 31. Group Functions • MIN ( column) • Returns minimum value in column. • MIN (marks) from students 10 • SELECT student_name from students • WHERE marks = MIN(marks) FreakDeveloper
  32. 32. Group Functions • SUM ( column) • Returns sum of column. • SUM (quanitity) from orders 900 • SELECT SUM(quantity) from orders • WHERE cus_id= 7 FreakDeveloper
  33. 33. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  34. 34. Group Functions • CURRDATE() • CURRTIME() • SYSDATE() • NOW() • Returns the current date or time. FreakDeveloper
  35. 35. Group Functions • ADDMONTHS() • ADDYEARS() • Adds months and year respectively. • SYSDATE() + 1 FreakDeveloper
  36. 36. Group Functions • MONTHS_BETWEEN(date1, date2) • Tells the number of months between date1 and date2. • MONTHS_BETWEEN( • TO_DATE ('2003/08/02', 'yyyy/mm/dd'), TO_DATE ('2003/06/02', 'yyyy/mm/dd') • )  2 FreakDeveloper
  37. 37. Group Functions • SYSTIMESTAMP() • CURRTIMESTAMP() • TIMESTAMP () • Returns current date and time as a timestamp. FreakDeveloper
  38. 38. Types of SQL Functions? • String Functions • Numeric Functions • String / Number Conversion Functions • Group Functions • Date and Time Functions • Date Conversion Functions FreakDeveloper
  39. 39. Group Functions • TO_CHAR ( date, format) • Converts date into string using format. • TO_CHAR(now(), ‘dd-mm-yyyy’)  16-8- 2014 FreakDeveloper
  40. 40. Group Functions • TO_DATE ( string, format) • Converts string into dateusing format. • TO_DATE ('2014/08/16', 'yyyy/mm/dd'), FreakDeveloper
  41. 41. I wanna create my Function  FreakDeveloper
  42. 42. User Defined Function Types • Scalar Function: Returns single value as a result of actions perform by function. • Inline Table-Valued Function: Returns a table variable as a result of actions perform by function. • Multi-Statement Table-Valued Function: Same as ITVF, differ in performance. FreakDeveloper
  43. 43. Creating Scalar Function • CREATE FUNCTION function_name ( @parameter_name parameter_data_type ) RETURNS return_data_type AS BEGIN function_body RETURN scalar_expression END FreakDeveloper
  44. 44. Creating Scalar Function • CREATE FUNCTION function_name ( • @parameter_name parameter_data_type • RETURNS TABLE • AS • BEGIN • RETURN (Select Statement) • END FreakDeveloper
  45. 45. Questions? FreakDeveloper
  46. 46. Reference • http://msdn.microsoft.com/en-us/library/ms186755.aspx • http://www.dotnet- tricks.com/Tutorial/sqlserver/KY3T010412-Different-Types-of- SQL-Server-Functions.html • http://www.cs.utexas.edu/~mitra/csSpring2012/cs327/lectur es/sqlFunc.html FreakDeveloper
  47. 47. SlideShare • http://msdn.microsoft.com/en-us/library/ms186755.aspx • http://www.dotnet- tricks.com/Tutorial/sqlserver/KY3T010412-Different-Types-of- SQL-Server-Functions.html • http://www.cs.utexas.edu/~mitra/csSpring2012/cs327/lectur es/sqlFunc.html FreakDeveloper
  48. 48. Thank you FreakDeveloper

×