[go: up one dir, main page]

0% found this document useful (0 votes)
10 views25 pages

SqlFunctions-Part1

Uploaded by

priyanshu9107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views25 pages

SqlFunctions-Part1

Uploaded by

priyanshu9107
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

Functions in MySql

MYSQL FUNCTIONS
A Function is a predefined set of statements (program) to
accomplish a particular specified task. The functions work
on passed arguments and then return a value.
In MySQL, there are many categories of functions
depending on what type of arguments they work on:

Single Row Functions – They work with a single record at a


time and return a result for every record they work on.
[ i.e if it works on 5 rows, 5 rows of result will appear. ]
Group functions – They work with data of multiple rows
and they return a single result value for that group of rows.
[i.e. if it works on 5 rows, 1 row of result will appear.
MYSQL FUNCTIONS
Categories of functions – depending on datatype of
parameters in work on:
• Numeric Functions
• String Functions
• Date & Time Functions
• Miscellaneous Functions
Numeric Functions – Single Row Function
Function name Syntax Example Output
Power() / Pow() - it returns n1 Power (n1, n2) Select power(3,4); 81
raised to power n2 Select pow(3,4); 81
Select pow(qty,2)
from t2;

Mod() – it returns remainder on Mod(n1, n2) Select mod(49, 6); 1


n1 / n2
Sqrt() - it returns square root of Sqrt(n) Select sqrt(10); 3.1622
given number. Select sqrt(29.3); 5.4129
Numeric Functions – Single Row Function
Function name Syntax Example Output
Round() – it returns the fractional Round(n1 ,n2) Select round(4567.672, 1); 4567.7
number rounded upto specified no. of Note: n2 is optional. It defines Select round(4567.672 , 0); 4568
places. [OR a whole number if n2 is the no. of places upto which Select Round(4567.211) 4567
not specified] the number is to be rounded.
If n2 is -ive , it means no Select Round(4567.298, -2) 4600
decimal place is required but to Select Round(4537.298, -2) 4500
the left of decimal, whole
number upto n decimal places is
required keeping in rounding
policy in consideration.

Truncate() - it returns the Truncate(n1,n2) Select truncate (4567.672, 4567.6


1);
fractional number upto Zero Note: n2 is compulsory. It defines
the no. of places upto which the Select truncate (4567.672); 4567
or specified no. of places w/o number is to be truncated. Select truncate(4567.56,-2) 4500
rounding If n2 is -ive , it means no decimal
place is required but to the left of
decimal whole number upto n
decimal places is required. No
rounding policy is considered.
String Functions – Single Row Function
Lower() / Lcase()- it returns string in lower case
Syntax: Lcase(string / FieldName)
Lower(string / FieldName)
Eg:
Select lower(“ABC”);
Select lower(colname) from students;
String Functions – Single Row Function
Upper() / Ucase()- it returns string in CAPITAL case
Syntax: ucase(string / FieldName)
upper(string / FieldName)
Eg: Select ucase(string / Fieldname);
Select upper(Fname) from students;
String Functions – Single Row Function
Length(str/fieldname) – returns no. of characters in specified
parameter include spaces and any special character.
Syntax: Length(String / Fieldname)
Eg: Select length(“ Functions in MySQL “);
Select name, length(name) from students;
String Functions – Single Row Function
left(str/FieldName,n) – returns n characters from extreme left of
str / Fieldname Value
Syntax: left(string / FieldName , n)

Eg:
Select left(“ Function”,4) ; Fun
Select left(name,2) from students;
String Functions – Single Row Function
right(str/FieldName,n) – returns n characters from
extreme right of str / Fieldname Value
Syntax: right(string / FieldName , n)
Eg: Select right(“ Function”,4) ;
Select right(name,2) from students;
String Functions – Single Row Function
Substring/substr/mid(str,n1,n2)- returns n2 no. of characters from str starting from
n1 index no. n2 is optional, if not given it returns a string starting from character at
n1 index no to end of string. If n1 is a negative number, it starts from back of the
string. Note: [in SQL indexing starts from 1]

Syntax: select substring(“ String” / Fieldname,n1, n2);

Eg: select substring(“ABC-123”,2,4);


select mid(“ABC-123”,4);
select substr(name,4) from students;
String Functions – Single Row Function
Instr() – returns index no. of first occurrence of substr in given
str. [NOTE: index no starts from 1]
Syntax: Instr(str, substr)
Eg: Select INSTR(“Prevention is Better than Cure”, “e”) ;-> 3
Select instr(name,”et”) from students;l
String Functions – Single Row Function
Ltrim(str) – removes spaces from extreme left ANU Sharma
Rtrim(str) – removes spaces from extreme right
Trim(str) – removes spaces from extreme left & right both
Date Functions – Single Row Function
Curdate() – returns current date in yyyy-mm-dd format as a String Value
Now() – returns current date & time
Sysdate() – returns current date & time Difference b/w NOW() Vs SYSDATE()
NOW() function returns the time at
Eg. Select curdate(), now(), sysdate(); which the particular statement began to
execute whereas SYSDATE() function
returns the accurate time at which the
statement executes.

Note:Presentation
Recorded on 30/03/2020
Date Functions – Single Row Function
dayName(date) – Returns the name of Day for specified date i.e.
Sunday, Monday etc.
MonthName(date) - Returns the name of Month for specified date
i.e. January, February etc.
dayOfWeek(date) – Returns 1 – 7 depending upon day number for
week day. [Note: Sunday is day 1]
Date Functions
Date() – returns date part
– Single Row Function
Syntax: date(dateTimeStamp)
Eg: Select date(now());
Month(date) – Returns month number from specified date.
year(date) – Returns year part from specified date.
day(date) –returns day of month from given date.
dayOfYear(date) – Returns day number of the year from specified date.
Date Functions – Single Row Function
Hour(date) / minute(date) / second(date) –
returns hh, mm, ss from time portion of date

Note: Presentation Recorded on


30/03/2020 . Statement Executed
@ 4:15:34 p.m.
Aggregate Functions
AGGREGATE / STATISTICAL / GROUP / MULTI-ROW FUNCTIONS :
These functions work on a group of rows and return a single
value. They ignore null values.

Note: Table students with given data will be considered for Examples.
Aggregate Functions
Sum(fieldName)-returns sum of given fields’ values.
Min(fieldName)-returns Lowest of given fields’ values.
Max(fieldName)-returns highest of given fields’ values.
Avg(fieldName)-returns average of given fields’ values.
Syntax:
Select groupFunctionName(ColName) from tablename [where <condition>];
Aggregate Functions
Count(*) – it counts number of records that are displayed in
specified select statement.
Aggregate Functions
Count(colname) – it returns counts of records that are have some
value under that column in specified select statement.
Aggregate Functions
Count(distinct ColName) – it counts number of distinct values i.e.
repeating value counted only once in specified select statement.

You might also like