SqlFunctions-Part1
SqlFunctions-Part1
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:
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]
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: 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.