SQL Functions
SQL Functions
smitha
Objective of this chapter
Distinguish between two types of functions.
State the syntax and working of most of the Numeric, String and
date/Time functions
FUNCTION
A function is a special type of predefined command set that performs
some operation and returns a single value.
Functions operate on zero,one,two or more values that are provided to
them.
The values that are provided to functions are called parameters or
arguments.
Functions are divided into 2 categories
Single row functions
Multiple row functions
Single row functions
Single row functions operate on a single value to return a single value.
They can accept one or more arguments but return only one result per
row.
String functions
Date and Time function
Numeric Functions
Multiple row functions
Multiple row functions operate on a set of rows to return a single value.
SUM()
AVG()
COUNT()
MAX()
MIN()
Single row functions-String functions
i)length() function
String functions operate on character type data.
String functions are used to extract, change, format or alter character
strings.
LENGTH(str) Returns the length of a column or a string in bytes.
1) mysql> SELECT LENGTH ('Informatics');
Result: 11
2) mysql> SELECT LENGTH(Name) FROM student;
2)Concat() function
CONCAT(str1, str2,...) Returns the string that results from concatenating
the arguments.
May have one or more arguments.
Ex: 1)mysql> SELECT CONCAT ('My', 'S', 'QL');
Result: 'MySQL‘
2) mysql> SELECT CONCAT('Class', NULL, 'XI');
Result: NULL
3)SELECT CONCAT(First_ Name,'',Last_Name) FROM Employee;
3)Instr() function
INSTR (str,substr)
Returns the position of the first occurrence of substring.
1)mysql> SELECT INSTR ('Informatics', 'for');
Result: 3
2)mysql> SELECT INSTR ('Computers', 'pet');
Result: 0
3) mysql> SELECT INSTR (last_name,'Kiran') FROM Employee;
4)LOWER(str) or LCASE(str)
Returns the argument (str) in lowercase i.e. it changes all the characters
of the passed string to lowercase.
mysql> SELECT LOWER ('INFORMATICS'); Result: 'informatics‘
mysql> SELECT LOWER(Last_Name) FROM Employee;
5)UPPER() or UCASE()
UPPER(str) or UCASE(str) Returns the argument in uppercase. i.e. it
changes all the characters of the passed string to uppercase.
mysql> SELECT UPPER ('Informatics'); Result: 'INFORMATICS‘
mysql> SELECT UPPER(Last_Name) FROM Employee;
6)LEFT()
LEFT(str,n) Returns the specified number of characters (n)from the left
side of string str.
mysql> SELECT LEFT('Informatics', 3); Result: 'Inf‘
mysql>select LEFT(first_name,3)FROM Employee;
7)RIGHT()
RIGHT(str,n) Returns the specified number of characters (n)from the
right side of string str.
a)mysql> SELECT RIGHT('Informatics', 4); Result: 'tics'
b) mysql> select RIGHT(first_name,3) FROM Employee;
8)LTRIM()
LTRIM(str) Removes leading spaces i.e. removes spaces from the left side
of the string str.
a) mysql> SELECT LTRIM (' Informatics'); Result: 'Informatics'
b) mysql> SELECT LTRIM(First_Name) FROM Employee;
9)RTRIM()
RTRIM(str) Removes trailing spaces i.e. removes spaces from the right
side of the string str.
a) mysql> SELECT RTRIM ('Informatics '); Result: 'Informatics'
b) mysql> SELECT RTRIM(First_Name) FROM Employee;
10)TRIM(str)
TRIM(str) Removes both leading and trailing spaces from the string str.
a)mysql> SELECT TRIM(' Informatics '); Result: 'Informatics'
b) mysql> SELECT TRIM(First_Name) FROM Employee;
11)SUBSTRING (str,m,n) Or MID(str,m,n)