sql functions

48
SQL Function FreakDeveloper

Upload: ammarbrohi

Post on 29-Jun-2015

76 views

Category:

Data & Analytics


0 download

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 Functions

TRANSCRIPT

Page 1: SQL Functions

SQL Function

FreakDeveloper

Page 2: SQL Functions

Function?

• Stored piece of program that Manipulates submitted data and return some value.

FreakDeveloper

Page 3: SQL Functions

Where use SQL Functions?

• SELECT StatementSELECT Ucase(name) FROM students

• WHERE StatementSELECT * FROM students WHERE substr(class,1,1)

FreakDeveloper

Page 4: SQL Functions

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

String Functions

• LOWER( string )

• All letters are changed to lowercase.

• AMMAR ammar

FreakDeveloper

LOWER ( s )

Page 6: SQL Functions

String Functions

• UPPER( string )

• All letters are changed to uppercase

• ammar AMMAR

FreakDeveloper

LOWER ( s )

Page 7: SQL Functions

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

String Functions

• LENGTH( string)

• Tells length of string

• LENGTH(‘Ammar’) 5

FreakDeveloper

Page 9: SQL Functions

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

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

String Functions

• LTRIM( string, characters)

• Trims out string from left.

• LTRIM(‘ Ammar’) ‘Ammar’

• LTRIM(‘Ammar’, ‘m’) ‘Aar’

FreakDeveloper

Page 12: SQL Functions

String Functions

• RTRIM( string, characters)

• Trims out string from right.

• RTRIM(‘Ammar ’) ‘Ammar’

• RTRIM(‘Ammar’, ‘r’) ‘Amm’

FreakDeveloper

Page 13: SQL Functions

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

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

Numeric Functions

• ABS ( value)• Returns absolute value

• ABS(-13) 13

FreakDeveloper

Page 16: SQL Functions

Numeric Functions

• MOD( value, divisor)• Returns remainder of value divided by

divisor.

• MOD(4,2) 0

FreakDeveloper

Page 17: SQL Functions

Numeric Functions

• POWER( value, exp)• Raises power of value to its exp

• POWER(2,2) 4

FreakDeveloper

Page 18: SQL Functions

Numeric Functions

• ROUND ( value, decimal)• Rounds value to decimal places.

• ROUND (20.6, 0) 21

FreakDeveloper

Page 19: SQL Functions

Numeric Functions

• CEIL ( value)

• Rounds value upwards.

• CEIL(20.6, 0) 21• CEIL(-20.6, 0) 20

FreakDeveloper

Page 20: SQL Functions

Numeric Functions

• FLOOR ( value)

• Rounds value downwards.

• FLOOR(20.6, 0) 20• FLOOR(-20.6, 0) 21

FreakDeveloper

Page 21: SQL Functions

Numeric Functions

• SQRT ( value)

• Returns Square root of value.

• SQRT(36) 6• SQRT(81) 9

FreakDeveloper

Page 22: SQL Functions

Numeric Functions

• SQRT ( value)

• Returns Square root of value.

• SQRT(36) 6• SQRT(81) 9

FreakDeveloper

Page 23: SQL Functions

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

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

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

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

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

Group Functions

• AVG ( column)

• Finds average of column

• AVG (rent) $1300

FreakDeveloper

Page 29: SQL Functions

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

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

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

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

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

Group Functions

• CURRDATE()• CURRTIME()• SYSDATE()• NOW()

• Returns the current date or time.

FreakDeveloper

Page 35: SQL Functions

Group Functions

• ADDMONTHS()• ADDYEARS()

• Adds months and year respectively.

• SYSDATE() + 1

FreakDeveloper

Page 36: SQL Functions

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

Group Functions

• SYSTIMESTAMP()• CURRTIMESTAMP()• TIMESTAMP ()

• Returns current date and time as a timestamp.

FreakDeveloper

Page 38: SQL Functions

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

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

Group Functions

• TO_DATE ( string, format)

• Converts string into dateusing format.

• TO_DATE ('2014/08/16', 'yyyy/mm/dd'),

FreakDeveloper

Page 41: SQL Functions

I wanna create my Function

FreakDeveloper

Page 42: SQL Functions

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

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

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

Questions?

FreakDeveloper

Page 48: SQL Functions

Thank you

FreakDeveloper