This document discusses different types of SQL functions including string, numeric, conversion, group, date/time, and user-defined functions. It provides examples of common string functions like UPPER, LENGTH, SUBSTR. Numeric functions covered include ABS, ROUND, POWER. Group functions include AVG, COUNT, MAX, MIN, SUM. Date functions allow conversion and calculation involving dates. The document demonstrates how to create scalar and table-valued user-defined functions in SQL.
2. Function?
• Stored piece of program that
Manipulates submitted data
and return some value.
FreakDeveloper
3. Where use SQL Functions?
• SELECT Statement
SELECT Ucase(name) FROM students
• WHERE Statement
SELECT * FROM students WHERE
substr(class,1,1)
FreakDeveloper
4. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
5. String Functions
• LOWER( string )
• All letters are changed to lowercase.
• AMMAR ammar
FreakDeveloper
LOWER ( s )
6. String Functions
• UPPER( string )
• All letters are changed to uppercase
• ammar AMMAR
FreakDeveloper
LOWER ( s )
14. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
23. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
24. Numeric Functions
• NANVL ( value, format)
• If value NaN, it will show replace_with
• NANVL(‘Ammar’, 15) 15
• NANVL(10, 15) 10
FreakDeveloper
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. Numeric Functions
• TO_NUMBER ( string, format)
• Converts string into number using
format.
• TO_NUMBER(‘100.11’, 999.9) 100.1
FreakDeveloper
27. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
28. Group Functions
• AVG ( column)
• Finds average of column
• AVG (rent) $1300
FreakDeveloper
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. 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. 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. Group Functions
• SUM ( column)
• Returns sum of column.
• SUM (quanitity) from orders 900
• SELECT SUM(quantity) from orders
• WHERE cus_id= 7
FreakDeveloper
33. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
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
38. Types of SQL Functions?
• String Functions
• Numeric Functions
• String / Number Conversion Functions
• Group Functions
• Date and Time Functions
• Date Conversion Functions
FreakDeveloper
39. Group Functions
• TO_CHAR ( date, format)
• Converts date into string using format.
• TO_CHAR(now(), ‘dd-mm-yyyy’) 16-8-
2014
FreakDeveloper
40. Group Functions
• TO_DATE ( string, format)
• Converts string into dateusing format.
• TO_DATE ('2014/08/16', 'yyyy/mm/dd'),
FreakDeveloper
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. 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. Creating Scalar Function
• CREATE FUNCTION function_name (
• @parameter_name parameter_data_type
• RETURNS TABLE
• AS
• BEGIN
• RETURN (Select Statement)
• END
FreakDeveloper