Sei sulla pagina 1di 6

visit @ http://www.infotechcomputer.

in

Chapter 15 MySql Functions


Function : A function is a special type of predefined command set that performs some operation and
returns a single value.
There are various functions in mysql which are categorized as follows:

String Functions
1. Char () - This function returns the character for each integer passed
eg1. select char(70,65,67,69)
65 = A
97 = a
Ans - FACE
eg2. select char(65,67.3,'69.3')
ans - ACE
2. Concat() - This function concatenates two strings
eg1. select concat("ram","sharma")
ans - ramsharma
ans - mohan das
eg2. select concat(name,city) from emp
ans - ramajmer
shyamjaipur
mohanbeawar......
eg3. select concat(concat(name," lives in "),city) from emp
ans - ram lives in ajmer
shyam lives in jaipur
mohan lives in beawar
3. Lower/Lcase() - This function converts a string into lowercase
eg1. select lower("RaMeSh")
ans - ramesh
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 1

visit @ http://www.infotechcomputer.in
ans - sarthak
eg2. select lower(ename) from emp
4. Substr() : This function extract a substring from a given string
eg1. select substr("infotech",5,4)
ans - tech
eg2. select substr(ename,3,4) from emp
eg3. select substr('abcdefg',-5,4)
ans - cdef
5. Upper/ucase() - This function converts the given string into upper case
eg1. select upper('infOTEch')
ans - INFOTECH
eg2. select upper(ename) from emp;
6. Ltrim() - this function removes leading spaces i.e. spaces from the left of given string
eg1. select ltrim(' RDBMS mysql')
ans - RDBMS mysql (without spaces)
eg2. select ltrim(ename) from emp;
7. Rtrim() - this function removes trailing spaces i.e. spaces from the right of given string
eg1. select rtrim('RDBMS mysql ')
ans - RDBMS mysql (without spaces)
eg2. select rtrim(ename) from emp;
8. Trim() - this function removes leading and trailing spaces from a given string.
eg1. select trim(' RDBMS mysql ')
ans - RDBMS mysql (without spaces)
eg2. select trim(ename) from emp;
eg3. select trim(leading 'x' from 'xxxxxBarOnexxxxxx')
ans - Bar Onexxxxxx
eg4. select trim(trailing 'x' from 'xxxxxxBarOnexxxxxx')
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 2

visit @ http://www.infotechcomputer.in
ans - xxxxxxBarOne
eg5. select trim(both 'x' from 'xxxxxxBarOnexxxxxx')
ans - BarOne
9. Instr() - This function searches for given secnd string into the given first string.
eg1. select instr('Corporate Floor','or')
ans - 2
eg2. select job,instr(job,'LE') from emp
10. Length() - This function returns length of a given string in bytes
eg1. select length('infotech')
ans - 8
eg2. select length(ename) from emp
11. Left() - This function returns the leftmost number of characters as specified
eg1. select left('infotech',4)
ans - info
eg2. select left(ename,3) from emp

Numeric Functions
1. Mod() : This function returns modulus (remainder) of given two numbers
eg. select mod(11,4)
ans - 3
eg2. select mod(salary,100) from emp;
2. Power/Pow() : This function returns x to the power y.
eg1. select pow(2,5)
ans - 32
eg2. select pow(sal,2) from emp;
3. Round() : This function returns a number rounded off as per given specifications
eg1. select round(15.93,1)
ans - 15.9

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 3

visit @ http://www.infotechcomputer.in
eg2. select round(15.938,2)
ans - 15.94
eg3. select round(23.455,0)
ans - 23
eg4. select round(34.4533,-1)
ans - 30
eg5. select round(3455.335,-3)
ans 3500
4. Sign() : This function returns sign of given number.
eg1. select sign(34)
ans - 1
eg2. select sign(-34)
ans - -1
eg3. select sign(0)
ans - 0
5. Sqrt() : This function returns the square root of given number
eg1. select sqrt(81)
ans - 9
eg2. select sqrt(sal) from emp
6. Truncate() : This function returns a number with some digits truncated
eg1. select truncate(14.322,1)
ans - 14.3
eg2. select truncate(14.399,1)
ans - 14.3
eg2. select truncate(234.345,-1)
ans - 230

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 4

visit @ http://www.infotechcomputer.in
Date and Time Functions
1. Curdate() / Current_date() : This function returns the current date
eg1. select curdate()
ans - 1 Jan 2013
eg2. select curdate()+10
ans - 11 Jan 2013
2. Date() : This function extracts the date part of a date or datetime expression
eg1. select date('2012-01-05 01:05:50')
ans - 2012-01-05
3. Month() - This function returns the month from the date passed
eg1. select month('2010-05-04')
ans - 5
4. Year() - This function return year part of the date
eg1. select year('2009-02-03')
ans - 2009
5. Dayname() - This function returns the name of weekday
eg1. select dayname('2009-02-03')
ans - Tuesday
eg2. select dayname(curdate())
ans - Tuesday
6. DayofMonth() : This function returns the day of month
eg1. select dayofmonth('2009-02-03')
ans - 3
eg2. select dayofmonth(curdate())
ans - 1
7. Dayofweek() : This function returns the day of week
eg1. select dayofweek('2009-02-13')
ans - 6
eg2. select dayofweek(curdate())
ans - 2
INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 5

visit @ http://www.infotechcomputer.in

8. DayofYear() : This function returns the day of year


eg1. select dayofyear('2009-02-13')
ans - 44
eg2. select dayofyear(curdate())
ans - 1
9. Now() : This function returns the current date and time
eg1. select now()
ans - 2013-01-01 4:41:52
10. Sysdate() : This function returns the time at which the function executes
eg1. select now(),sleep(2),now()
ans - 2009-06-03 15:32:34
0
2009-06-03 15:32:34
eg2. select sysdate(),sleep(2),sysdate()
ans - 2009-06-03 15:33:15
0

2009-06-03 15:33 :17

INFOTECH Computer Education, Kishangarh (Raj) 9829171122

Page 6

Potrebbero piacerti anche