sql functions
DESCRIPTION
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 FunctionsTRANSCRIPT
![Page 1: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/1.jpg)
SQL Function
FreakDeveloper
![Page 2: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/2.jpg)
Function?
• Stored piece of program that Manipulates submitted data and return some value.
FreakDeveloper
![Page 3: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/3.jpg)
Where use SQL Functions?
• SELECT StatementSELECT Ucase(name) FROM students
• WHERE StatementSELECT * FROM students WHERE substr(class,1,1)
FreakDeveloper
![Page 4: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/4.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 5: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/5.jpg)
String Functions
• LOWER( string )
• All letters are changed to lowercase.
• AMMAR ammar
FreakDeveloper
LOWER ( s )
![Page 6: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/6.jpg)
String Functions
• UPPER( string )
• All letters are changed to uppercase
• ammar AMMAR
FreakDeveloper
LOWER ( s )
![Page 7: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/7.jpg)
String Functions
• INSTR( string, find, pos, occ)
• Finds string within string.
• INSTR(‘Ammar’, ‘ar’) 4
• INSTR(‘Ammar’, ‘a’,1,2) 4
FreakDeveloper
![Page 8: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/8.jpg)
String Functions
• LENGTH( string)
• Tells length of string
• LENGTH(‘Ammar’) 5
FreakDeveloper
![Page 9: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/9.jpg)
String Functions
• LPAD( string, length, pad-string)
• Adds string to its left.
• LPAD(‘Ammar’,10) ‘ Ammar’
• LPAD(‘Ammar’,10, ‘-’) ‘-----Ammar’
FreakDeveloper
![Page 10: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/10.jpg)
String Functions
• RPAD( string, length, pad-string)
• Adds string to its right.
• RPAD(‘Ammar’,10) ‘Ammar ’
• RPAD(‘Ammar’,10, ‘-’) ‘Ammar-----’
FreakDeveloper
![Page 11: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/11.jpg)
String Functions
• LTRIM( string, characters)
• Trims out string from left.
• LTRIM(‘ Ammar’) ‘Ammar’
• LTRIM(‘Ammar’, ‘m’) ‘Aar’
FreakDeveloper
![Page 12: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/12.jpg)
String Functions
• RTRIM( string, characters)
• Trims out string from right.
• RTRIM(‘Ammar ’) ‘Ammar’
• RTRIM(‘Ammar’, ‘r’) ‘Amm’
FreakDeveloper
![Page 13: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/13.jpg)
String Functions
• SUBSTR( string, pos, number)
• Takes out string from string.
• SUBSTR(‘Ammar’, 3) ‘mar’
• SUBTR(‘Ammar’, 3, 2) ‘ma’
FreakDeveloper
![Page 14: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/14.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 15: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/15.jpg)
Numeric Functions
• ABS ( value)• Returns absolute value
• ABS(-13) 13
FreakDeveloper
![Page 16: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/16.jpg)
Numeric Functions
• MOD( value, divisor)• Returns remainder of value divided by
divisor.
• MOD(4,2) 0
FreakDeveloper
![Page 17: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/17.jpg)
Numeric Functions
• POWER( value, exp)• Raises power of value to its exp
• POWER(2,2) 4
FreakDeveloper
![Page 18: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/18.jpg)
Numeric Functions
• ROUND ( value, decimal)• Rounds value to decimal places.
• ROUND (20.6, 0) 21
FreakDeveloper
![Page 19: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/19.jpg)
Numeric Functions
• CEIL ( value)
• Rounds value upwards.
• CEIL(20.6, 0) 21• CEIL(-20.6, 0) 20
FreakDeveloper
![Page 20: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/20.jpg)
Numeric Functions
• FLOOR ( value)
• Rounds value downwards.
• FLOOR(20.6, 0) 20• FLOOR(-20.6, 0) 21
FreakDeveloper
![Page 21: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/21.jpg)
Numeric Functions
• SQRT ( value)
• Returns Square root of value.
• SQRT(36) 6• SQRT(81) 9
FreakDeveloper
![Page 22: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/22.jpg)
Numeric Functions
• SQRT ( value)
• Returns Square root of value.
• SQRT(36) 6• SQRT(81) 9
FreakDeveloper
![Page 23: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/23.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 24: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/24.jpg)
Numeric Functions
• NANVL ( value, format)
• If value NaN, it will show replace_with
• NANVL(‘Ammar’, 15) 15• NANVL(10, 15) 10
FreakDeveloper
![Page 25: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/25.jpg)
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
![Page 26: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/26.jpg)
Numeric Functions
• TO_NUMBER ( string, format)
• Converts string into number using format.
• TO_NUMBER(‘100.11’, 999.9) 100.1
FreakDeveloper
![Page 27: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/27.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 28: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/28.jpg)
Group Functions
• AVG ( column)
• Finds average of column
• AVG (rent) $1300
FreakDeveloper
![Page 29: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/29.jpg)
Group Functions
• Count ( column)
• Returns number of rows.
• Count (*) from orders 140
• Count (customer_id) from orders • WHERE customer_id=4 9
FreakDeveloper
![Page 30: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/30.jpg)
Group Functions
• MAX ( column)
• Returns maximum value in column.
• MAX(marks) from students 99
• SELECT student_name from students• WHERE marks = MAX(marks)
FreakDeveloper
![Page 31: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/31.jpg)
Group Functions
• MIN ( column)
• Returns minimum value in column.
• MIN (marks) from students 10
• SELECT student_name from students• WHERE marks = MIN(marks)
FreakDeveloper
![Page 32: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/32.jpg)
Group Functions
• SUM ( column)
• Returns sum of column.
• SUM (quanitity) from orders 900
• SELECT SUM(quantity) from orders• WHERE cus_id= 7
FreakDeveloper
![Page 33: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/33.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 34: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/34.jpg)
Group Functions
• CURRDATE()• CURRTIME()• SYSDATE()• NOW()
• Returns the current date or time.
FreakDeveloper
![Page 35: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/35.jpg)
Group Functions
• ADDMONTHS()• ADDYEARS()
• Adds months and year respectively.
• SYSDATE() + 1
FreakDeveloper
![Page 36: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/36.jpg)
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
![Page 37: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/37.jpg)
Group Functions
• SYSTIMESTAMP()• CURRTIMESTAMP()• TIMESTAMP ()
• Returns current date and time as a timestamp.
FreakDeveloper
![Page 38: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/38.jpg)
Types of SQL Functions?
• String Functions• Numeric Functions• String / Number Conversion Functions• Group Functions• Date and Time Functions• Date Conversion Functions
FreakDeveloper
![Page 39: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/39.jpg)
Group Functions
• TO_CHAR ( date, format)
• Converts date into string using format.
• TO_CHAR(now(), ‘dd-mm-yyyy’) 16-8-2014
FreakDeveloper
![Page 40: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/40.jpg)
Group Functions
• TO_DATE ( string, format)
• Converts string into dateusing format.
• TO_DATE ('2014/08/16', 'yyyy/mm/dd'),
FreakDeveloper
![Page 41: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/41.jpg)
I wanna create my Function
FreakDeveloper
![Page 42: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/42.jpg)
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
![Page 43: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/43.jpg)
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
![Page 44: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/44.jpg)
Creating Scalar Function
• CREATE FUNCTION function_name ( • @parameter_name parameter_data_type • RETURNS TABLE • AS • BEGIN• RETURN (Select Statement)• END
FreakDeveloper
![Page 45: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/45.jpg)
Questions?
FreakDeveloper
![Page 46: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/46.jpg)
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/lectures/sqlFunc.html
FreakDeveloper
![Page 47: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/47.jpg)
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/lectures/sqlFunc.html
FreakDeveloper
![Page 48: SQL Functions](https://reader035.vdocuments.site/reader035/viewer/2022062406/559139181a28ab14498b473d/html5/thumbnails/48.jpg)
Thank you
FreakDeveloper