0% found this document useful (0 votes)
21 views

Functions in SQL

Uploaded by

ZEEL SATVARA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Functions in SQL

Uploaded by

ZEEL SATVARA
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Functions in SQL

There are many build in functions available in Mysql


like string functions, numeric functions ,date and time
function tec.
String functions
Upper () : It returns all alphabets in upper case eg.
Select upper(‘narsee monjee college’);
NARSEE MONJEE COLLEGE
Lower() :It returns all alphabets in lower case eg. Select
lower(‘NM COLLEGE’);
 nm college
Concat(): it returns the string obtained by combining
the given set of stings one after the other.
Eg . Select concat(‘narsee’, ‘ ‘ , ‘monjee’, ‘ ‘ , ‘college’);
narsee monjee college
String functions
Concat_ws(): ws stands for with separator and is
special form of concat()
Eg select concat_ws(‘-’ ,’MY’, ‘S’ ,’Q’, ‘L’);
MY-S-Q-L
Left(str,n) : returns the leftmost ‘n’ characters from
the string
Eg select left(‘narsee monjee’, 4);
nars

sunil kadam
String functions
Right (str,n) : returns the righttmost ‘n’ characters
from the string
Eg select right(‘narsee monjee’, 4);
njee
Substring(str,m,n): returns a string of ‘n’ char long
from given string starting from ‘m’ position.
E.g select substring(‘narsee monjee college’,4,6)
see mo
String functions
LTRIM(str) : returns the leading (starting ) blank
spaces removed.
Eg. select Ltrim(‘bbbbnarsee’);
narsee
RTRIM(str) : returns the trailing (ending ) blank
spaces removed.
Eg. select Rtrim(‘narseebbbb);
narsee
String functions
TRIM(str) :returns the string with all blank stirng
prefixes and suffixes removed.
Eg. select trim(‘bbbbnarseebbbb);
narsee
String functions
Lpad (str,n,padstr) : returns the string ,left padded with
the pad string until string is ‘n’ char long.
Eg select lpad(‘hi’,6,’!’);
!!!!hi
Eg select lpad(100,7,’$’);
 $$$$100
If string is longer than ‘n’ then it will be shortened to n
char
Select lpad (‘It is mysql ‘ ,8 , ’#’);
It is my
String functions
Rpad(str,n,padstr): returns the string ,right padded
with the pad string until string is ‘n’ char long.
Eg select rpad(‘hi’,6,’*’);
Hi****
Eg select rpad(100,7,’$’);
100$$$$
String functions
Length (str): returns the length of a string,
Eg select length(‘narsee monjee’);
13
Reverse (str) returns string in the reverse order
Eg select reverse(‘college’);
egelloc
String functions
Repeat (str,n): returns a string containing repeated ‘n’
times
Eg select repeat(‘@’,10);
@@@@@@@@@@
Numeric functions

 Abs(n):returns the absolute value of n


 Eg select abs(-23);
 23
 select abs(23);
 23
 Sign(n): returns the sign of the argument as -1,0,1 depending on
whether n is negative, zero,positive.
 Eg select sign(-22);
 -1
 select sign(233);
1
 select sign (0);
0
Numeric functions

Mod(n,m): returns the remainder of n divided by m


Select mod(12,5);
Gives 2
Select mod(10,-3);
 gives 1
Select mod(-10,3);
gives -1
Select mod(-10,-3);
gives -1
Select mod(34.4,3);
Numeric functions

Round(x,n): returns the x rounded to a number


with n decimals
Select round(1.298, 1);
1.3
Select round(1.298,0);
1
Numeric functions
If n is negative then, for -1  nearest 10,-2 nearest 100 
Select round(55,-1)
60
Select round (126,-2);
100
Select round(495,-2);
500
Select round(495,-3);
0
Numeric functions

Pow(x,y) or power(x,y):returns the value of x raised to


power y
Eg select pow(2,4);
16
Select power(2,10);
1024
Numeric functions

Sqrt(n): return the square root of n


Select sqrt(16);
4
Select sqrt(20);
4.472135954999958
Select sqrt(-16);
Null
Select sqrt(abs(-16));
4
Numeric functions
Floor(x) : returns the largest integer value,which is not
greater than x , in other words floor will round off the
given number to the nearest lower integer.
Eg select floor(1.23);
1
Select floor (-1.23);
-2
Select floor (1.99)
1
Numeric functions
Ceiling (x): returns the smallest integer value,which is
not less than x , in other words ceiling will round off
the given number to the nearest upper integer.
Eg select ceiling(1.23);
2
Select ceiling(-1.23);
-1
Numeric functions

Least(x,y…): with 2 or more arguments, returns the


smallest argument
Select least(2,9,-2);
-2
Select least(‘a’,’b’,’c’);
a
Numeric functions
Greatest (x,y…): with more than 2 arguments returns
the maximum of the argument
Select greatest(23,8,99);
99
Select greatest(‘sunil’,’jay’,’anil’);
Sunil
Select greatest (‘B’,’a’,’C’);
C
Date and Time functions
Curdate(): It returns the current date of the system
Eg. Select curdate();
Curtime(): It returns the current sytem time.
Now(): It displays the current date and time of
system.The function does not need any argument
Timestamp(): It displays the date and time . The
function need any date as argument.
Date and Time functions
Year(): It returns the year part of the given date
Eg. Select year(curdate());
2015
Month(): It returns the month of the given date.
Eg. Select month(curdate());
7
Monthname(): It displays the full name of the given
month
Eg. Select monthname(curdate());
july
Date and Time functions
Day(): It displays the date part of the given date.
Eg. select day(curdate());
Dayname(): It gives the weekday of the week inwords
like Monday,Tuesday…etc of the given date.
Eg select dayname(curdate());
Dayofweek() : It returns the weekday index (1-7) of
the given date like Sunday 1,Monday 2…Saturday 7
Dayofyear(): It returns the day of the year index (1-
366) of the give date like 4-feb as 35, 31-12-2009 as 365
and 31-12-2008 as 366
Date and Time functions
Time(): IT separates the time portion of the given expression
containing date and time.
Eg select time(now());
Hour():IT separates the hour portion of the given expression
containing date and time.
Eg select hour(now());
Minute():IT separates the minute portion of the given expression
containing date and time.
Eg select minute(now());
Second():IT separates the second portion of the given expression
containing date and time.
Eg select second(now());
Date and Time functions
Week(): It returns the week number of the given
expression
Eg select week(now());
Select week(curdate());
Aggregate functions
These functions are used for groups i.e. sets of values.
These are applied on column name of different tables.
1 AVG() : It returns the average value of the given
argument, which is a field name of a table. Distinct
option may be used to calculate avg of only distinct
values from the given table
Eg select avg(sal) from emp;
Aggregate functions
Count(): If returns a count of number of values given
in the row ,specified by a select statement. There is a
format count(*),giving the count of number of row or
records in table
Eg select count(*) from emp
Aggregate functions
Max(): It displays the maximum value from given set
of values,columns
Select max(f_name),max(sal) from emp;
Min(): It displays the minimum value from given set of
values,columns
Select min(f_name),min(sal) from emp;
Sum(): It displays the sum of the given set of
values,column.It is applicable only to numeric data
Eg. Select sum(sal) from emp;

You might also like