Chapter 3 Database Query Using SQL Functions
Chapter 3 Database Query Using SQL Functions
input
it is a set of SQL function can
Dasically,
and return the result. A
parameters, perform actions Functions a r e o t
alternate to
value or a table.
single
returh an only a command(generally
commands but are used as a part of sql
sqi
select command).
Types of Function (System defined) that is,
on scalar values
- -
POWER() :
returns the value of a number raised to the power of
anotheer
power()
number. The synonym of power() is pow().
Syntax pow(m,n)
m A number which is the base of the exponentiation.
POWER(2,B) I|
ROUNDb0
number rounded to a certain number of
The round() function returns a
decimal places.
Syntax - ROUND(column_name,decimals)
The field to round.
column_name: Required
to be returned.
Decimals: Required Specifies the number of decimals
Created by. Abhay
integer ,if its
next right
value is rounded to next
Decimal places position
side number is>=5
if we do not specify
Default decimal place is 0 position
mys1 sELECT OUND(3.72,2)
my sl$ELECT ROUND (36.4) ..-4---
---- ROUND( 36.725 2)
ROUND 36.4)
+-
36 73
36
TRUNCATE()
Page 8 Created by: Abhay
The truncate() function returns a number to a certain number of
decimal placesS.
Syntax TRUNCATE(number,decimals)
number : Required The field to round.
Decimals: Required Specifies the number of decimals to be returned.
Syntax MOD(dividend,divisor)
Dividend -
mysl>SELECT OD (10.5,3)
MDD ( 10.53)
UPPER(str)
to uppercase.
string str with all characters changed
Returns the
mysql> SELECT UPPER(Tej);
TEJ
UCASE(str)-UCASE() is a synonym for UPPER().
LOWER(str)-
changed to lowercase
Returns the string str with all characters
mysql> SELECT LOWER("'QUADRATICALLY");
SUBSTRING(str, pos)
SUBSTRING(str, pos,len)- SUBSTRING(str FROM pos FOR len)
The forms without a len argument return a substring from string str
starting at position pos. The forms with a len argument return a
substring len characters long from string str, starting at position pos.
The forms that use FROM are standard SQL syntax. It is also possible
to use a negative value for pOs. In this case, the beginning of the
substring is pos characters from the end of the string, rather than the
beginning.
mysql> SELECT SUBSTRING('practically',5);
MID(str,pos,len)
synonym for SUBSTRING(str,pos,len),
substr()
MID(str, pos, len) is a
LENGTH(str)
Created by: Abhay
Returns the length of the string str
mysql> SELECT LENGTH('text);
>4
LEFT(str,len)
the string str, or NULL if any
Returns the leftmost len characters from
argument is NULL.
mysql> SELECT LEFT("Toolbar', 4);
->Tool'
RIGHT(str,len)
Returns the rightmost len characters from the string str, or NULL if
any argument is NULL.
mysql> SELECT RIGHT("Toolbar, 3);
->'bar'
Created by: Abhay
INSTR(str,substr)-
Returns the position of the first occurrencee of substring substr in
string str.
mysql> SELECT INSTR('Toobarbar, 'bar);
->4
mysql> SELECT INSTR('xbar', 'ybar";
->0
LTRIM(str)
Returns the string str with leading space characters removed.
mysql> SELECT LTRIM(' Toolbar");
>Toolbar'
FROM] Str) If
str with all remstr prefixes or suffixes removed.
Returns the string BOTH
LEADING, or TRAILING is given ,
n o n e of the specifiers BOTH,
is assumed.
mysql> SELECT TRIM tool ');
->"bar'
'x' FROM 'xxxtooixxx');
mysql> SELECT TRIM(LEADING
->toolxxx'
FROM 'xxxtoolxxx');
mysql> SELECT TRIM(BOTH '*'
->tool'
Created by: Abhay
mysql> SELECT TRIM(TRAILING
->tool' 'xyz' FROM 'toobxxx');
Date functions-
Perform operation over date values.
NOW()
Returns the current date and time as a value in "YYYY-MM-DDb
the
hh:mm:ss' or YYYYMMDDhhmmss format, depending on whether
function is used in string or numeric context.
mysql> SELECT NOW);
->2020-04-0523:50:26'
+0;
mysql> SELECT NOW()
Created by: Abhay
20200415235026.000000D
Here +0 means +0 second
MONTH(date)-
Dofturns the month for date, in the
December, 0 for dates range 1 to 12 for
or
such as
'0000-00-00' January to
or 2008-00-00'
-00-00' that
Created by: Abhay
have a zero month part.
mysql> SELECT MONTH(2008-02-03);
-2
MONTHNAME(date)-
Returns the full name of the month for date.
zero" date.
Created by: Abhay
mysql> SELECT YEAR('1987-01-01');
->1987
DAY(date)-
Returns the day of the month for date, in the range 1 to 31, or 0 for
dates such as '0000-00-00' or '2008-00-00' that have a zero day part.
mysql> SELECT DAYOFMONTH('2007-02-03');
-3
DAYNAME(date)
Returns the name of the weekday for
date.
mysql> SELECT DAYNAME( 2007-02-03'):
-'Saturday'
DAYOFYEAR(date)-
date.
number of the day of year for given
Returns the
mysql> SELECT DAYNAME('2007-02-03');
'Saturday'
Created by: Abhay
Aggregate Functions
mysql> Select Job, Min(Pay),Max(Pay), Avg(Pay) from EMP Group By Job HAVING
Sum(Pay)>=8000;
mysql> Select Job, Sum(Pay) from EMP Where City="Jaipur
Note:-Where clause works in respect of whole table but Having works on
Group only. If Where and Having both are used then Where will be executed first.
mysql> SELECT
*
FROM Student ORDER BY City;
mysql> SELECT Name, Fname, City FROM Student Where Name LIKE 'R% ORDER
BY Class;