Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 8

----------------------------------------------------------------Mathematical

Functions in SQL---------------------------------------------------
Mathematical functions are very important in SQL to implement different
mathematical concepts in queries.

1. MOD():-
The variable X is divided by Y and their remainder is returned. For example −

example-
Select mod(9,5);

Example - Fetch Even / Odd employee_id from employees table

SELECT * FROM hr.Employees


WHERE MOD(employee_id,2)= 1

2. FLOOR():- This function is used to get the smallest integer value, which is less
than or equal to the given numeric expression.

Example- SELECT FLOOR(10.5) FROM DUAL

3.CEIL():-

This function is used to get the largest integer value, which is greater than or
equal to the given numeric expression.

Example-
Select ceil(5.7)

4. ROUND()-

This function is used to get the nearest numeric value, which is rounded to the
specified length or precision.

Example-
SELECT ROUND(5.4) FROM DUAL
SELECT ROUND(5.6) FROM DUAL

5. SQRT()-
This function returns the square root of

Example-
SELECT SQRT(9) FROM DUAL
SELECT SQRT(10) FROM DUAL

6. POWER()-
This function is used to get the power of user-specified expression or value.

SELECT POWER(2,5) FROM DUAL

7. NVL2():-
If first parameter is null then it returns 3rd parameter , if first parameter is
not null then it returns
2nd parameter.

Syntax:
NVL2(parameter1,parameter2,parameter3);
Example-
SELECT NVL2(NULL,1,2)"NVL2" FROM dual;

SELECT NVL2(1,2,3)"NVL2" FROM dual;

SELECT NVL2(1,NULL,3)"NVL2" FROM dual;

SELECT NVL2(1,2,NULL)"NVL2" FROM dual;

8. Nullif()-
If the 1st & 2nd values are same then it returns NULL , if two values are different
then it
returns first parameter.

Syntax:
NULLIF(value1,value2);
Example-
SELECT NULLIF(1,1)"Nullif" FROM dual;
SELECT NULLIF(1,2)"Nullif" FROM dual;

------------------------------------------------
Task----------------------------------------------------------------

-- Date Functions

-- The default format of date is 'DD-MON-YYYY'.... or we can check 'select Sysdate


from Dual;'.....

-- Following are the types of date functions:

1)SYSDATE 2)current_date
3)current_timestamp 4)SYSTIMESTAMP
5)local_timestamp 6)add_months
7)months_between 8)next_day
9)last_day 10)EXTRACT
11)to_char 12)to_date
13)GREATEST 14)LEAST
15)ROUND 16)TRUNC
17)new_time 18)COALESCE
19)DBTIMEZONE

--1). Sysdate: it returns current date of "system" in oracle date format.

--Ex:

SELECT SYSDATE FROM dual;

--2). Current_date: it returns the current date of "server/sessions timezone".

--Ex:

SELECT current_date FROM dual;

--3) current_timestamp: it returns the current timestamp with active time zone
information of server/database.

--Ex:

SELECT CURRENT_TIMESTAMP FROM dual;

--4) systimestamp: this will returns the sysdate , including fractional seconds and
time zone of the system.

SELECT SYSTIMESTAMP FROM dual;

--5). local timestamp: this will return local timestamp in the active time zone
information with no time zone
-- information shown.

SELECT LOCALTIMESTAMP FROM dual;

--6). Add_months: it is used to 'add or remove no of moths' from the specified date

--Syntax:
Add_months(DATE,no.of MONTHS)

SELECT add_months(SYSDATE,5) FROM dual;

SELECT add_months(SYSDATE,-2) FROM dual;

SELECT add_months(to_date('1-jan-2010','dd-mon-yyyy'),5) FROM dual;

SELECT add_months(to_date('2-2-2020','dd-mm-yyyy'),-2) FROM dual;

SELECT add_months(to_date('27/2/18','dd/mm/yy'),5) FROM dual;

SELECT add_months(hire_date,9) FROM employees;

-- 7) Months_between: it is used to give the difference between 'two dates'.

--Syntax:
Months_between('Date1','Date2')

SELECT months_between(to_date('12/jan/2020'),to_date('24/feb/2019')) "Months" FROM


dual;

SELECT months_between(to_date('12/01/2020','dd/mm/yyyy'),to_date('24/02/2019','dd/
mm/yyyy'))"Months" FROM dual;

SELECT months_between('12/jun/2010','23/mar/2019')/12 "Yrs" FROM dual;

SELECT TRUNC(months_between('4/sep/2019','23/mar/2018')/12)"Months as year" FROM


dual;

SELECT TRUNC(months_between(SYSDATE,hire_date)/12) "Exp_years"FROM employees;

-- 8). Next_day: it returns 'next day date' of the given day from specified date.

--Syntax:
next_day('DATE','Day')

SELECT next_day('13/apr/2020','Thursday') FROM dual;

--9). Last_day: it returns last date of the specified month.

--Syntax:
last_day ('DATE');

SELECT last_day(SYSDATE) FROM dual;

SELECT last_day('12/mar/2020') FROM dual;

--10) Extract: this is used to extract a portion of the date value.

--Syntax:
EXTRACT(YEAR/MONTH/DAY/HOUR/MINUTE FROM DATE);

SELECT EXTRACT(YEAR FROM SYSDATE) "Year" FROM dual;

SELECT EXTRACT(MONTH FROM SYSDATE) "Month" FROM dual;

SELECT EXTRACT(DAY FROM SYSDATE) "Date" FROM dual;

SELECT EXTRACT(YEAR FROM hire_date) "Years" FROM employees;

SELECT EXTRACT(MONTH FROM hire_date) "Months" FROM employees;

--11). To_char: it is used to extract various date formats.

-- The available date formats are:

D :- No.of days IN a week.


DD:- No.of days IN MONTH
DDD:- No.of days IN YEAR
MM:- No.of MONTH
MON:- Three letter abbreviation OF MONTH
MONTH:- Fully Spelled OUT MONTH
DY:- Three letter abbreviation OF DAY
DAY:- Fully spelled OUT DAY
Y:- LAST ON digit OF YEAR
YY:- LAST TWO digit OF YEAR
YYY:- LAST three digit OF YEAR
YYYY:- LAST four digit OF YEAR
SYYYY:- Signed YEAR
IYYY:- Four digit YEAR FROM iso STANDARD
Y,YYY:- YEAR WITH comma
YEAR:- Fully spelled OUT YEAR
CC:- Centuri
W:- NO. OF weeks IN a MONTH
HH:- Hours
MI:- Minutes
SS:- Seconds
TH:- Suffix TO a NUMBER
FF:- Fraction OF SECOND
-- Syntax:
To_char(DATE,format);

--Ex:
SELECT to_char(SYSDATE,'DAY/MONTH/YEAR') FROM dual;

SELECT to_char(SYSDATE,'MONTH') FROM dual;

SELECT to_char(hire_date,'DAY/YYYY') FROM employees;

SELECT (to_char(SYSDATE,'DDTH/MONTH/YYYY')) FROM dual;

-- Q) display the employee who are joined in December month from employees table?

SELECT * FROM employees WHERE to_char(hire_date,'MON')='DEC';

--12) To_date: it is used to convert string date into oracle date format.

SELECT to_date('27/march/2020') FROM dual;

--Q) to add five days from sysdate.

SELECT to_date(SYSDATE)+5 FROM dual;

--13) Greatest: it returns the Greatest date.

--Syntax:
GREATEST(date1,date2,date3);

--Ex:

SELECT GREATEST('08/mar/2019','07/mar/2020','4/jun/2012','3/jan/2013') FROM


dual;

--14) Least: it returns leasr date.

--Ex:

SELECT LEAST ('08/mar/2019','07/mar/2020','4/jun/2012','3/jan/2013') FROM dual;

--15) Round: it rounds the date to which it was equal to or greater than the given
date.

--Syntax:
ROUND(DATE,(DAY/MONTH/YEAR));

-- RULES FOR ROUND IN DATE FUNCTION:

1) IF THE SECOND parameter IS "YEAR" THEN ROUND will checks THE MONTH OF THE given
DATE IN THE
FOLLOWING ranges:
JAN -- JUN
JUN -- DEC

- IF THE MONTHS fall BETWEEN JAN & JUN THEN it returns THE FIRST DAY OF THE CURRENT
year.
- IF MONTHS falls BETWEEN THE JUN & DEC THEN it returns THE FIRST DAY OF NEXT year.

2) IF THE SECOND parameter was "Month" THEN ROUND will checks THE "DAY" OF THE
given DATE
IN THE FOLLOWING Ranges:

1 -- 15
15 -- 31

- IF THE DAY falls BETWEEN 1 & 15 THEN it returns THE FIRST DAY OF THE CURRENT
month.
- IF THE DAY falls BETWEEN 16 & 31 THEN it returns THE FIRST DAY OF NEXT month.

3) IF THE SECOND parameter was DAY THEN ROUND will checks THE week DAY OF THE given
DATE IN THE
FOLLOWING Ranges:

SUN -- WED
THUR -- SUN

-IF THE week days falls BETWEEN SUN & WED THEN it returns THE PREVIOUS sunday.

-IF THE week days falls BETWEEN THU & SUN THEN it returns THE NEXT sunday.

4) IF THE SECOND parameter IS NULL THEN it returns nothing.

5) IF you NOT specifying THE SECOND parameter THEN ROUND will resets THE TIME TO
THE begining OF THE CURRENT
DAY IN CASE OF USER specified date.

6) IF you are NOT specifying THE SECOND parameter THEN ROUND will RESETS THE TIME
TO THE begining OF THE
NEXT DAY IN CASE OF sysdate.

-- Ex:

SELECT ROUND(to_date('24/dec/2004'),'YEAR'),ROUND(to_date('11/mar/2006'),'year')
FROM dual;

SELECT ROUND(to_date('1/jan/2004'),'month'),ROUND(to_date('18/jan/2004'),'month')
FROM dual;

SELECT ROUND(to_date('26/dec/2006'),'day'),ROUND(to_date('29/dec/2006'),'day') FROM


dual;
--16) Trunc: Trunc will chops off the date to which it was equal to or less than
the given date.

-- Syntax:
TRUNC(DATE,(DAY/MONTH/YEAR));

-- Rules for Trunc in date function:

1) IF THE SECOND parameter IS YEAR THEN TRUNC returns FIRST DAY OF THE CURRENT
year.

2) IF THE SECOND parameter IS MONTH THEN TRUNC returns THE FIRST DAY OF THE CURRENT
month.

3) IF THE SECOND parameter IS DAY THEN TRUNC returns THE PREVIOUS sunday.

4) IF THE SECOND parameter was NULL THEN it returns nothing.

5) IF you NOT specify THE SECOND parameter THEN TRUNC will RESET THE TIME TO THE
beging OF THE CURRENT DAY.

--Ex:

SELECT TRUNC(to_date('24/dec/2004'),'YEAR'),TRUNC(to_date('11/mar/2006'),'YEAR')
FROM dual;

SELECT TRUNC(to_date('11/jan/2004'),'month'),TRUNC(to_date('18/jan/2004'),'month')
FROM dual;

SELECT TRUNC(to_date('26/dec/2006'),'day'),TRUNC(to_date('29/dec/2006'),'day') FROM


dual;

--17) New_time: this will give the desired timezone's date and time.

-- Syntax:
new_time(DATE,current_timezone,desired_timezone);

--TIMEZONES:

AST/ADT -- Atlantic standard/day light time


BST/BDT -- Bering standard/day light time
CST/CDT -- Central standard/day light time
EST/EDT -- Eastern standard/day light time
GMT -- Greenwich mean time
HST/HDT -- Alaska-Hawaii standard/day light time
MST/MDT -- Mountain standard/day light time
NST -- Newfoundland standard time
PST/PDT -- Pacific standard/day light time
YST/YDT -- Yukon standard/day light time

-- Ex:
SELECT to_char(new_time(SYSDATE,'GMT','YST'),'DD/MON/YYYY HH:MI:SS AM') FROM dual;

--18) Coalesce: It returns the first not null date.

--Ex:

SELECT COALESCE('12/jan/2010','13/jan/2010'),COALESCE(NULL,'7/feb/2010','28/march/
2009',NULL) FROM dual;

--19) DBtimezone: This will returns the current database in UTC(Co-ordinate


universal time) format.

SELECT DBTIMEZONE FROM dual;

-----------------------------------------------------------------------------------
------------------------------------------------------------------
--5) Conversion Function

--1) To_number: this will converts charecters to number.

--2) To_char: this is date conversion function & used to convert number or date to
char string.

--3) To_date: this also a date conversion function,this will converts number or
date into oracle default
-- Date format.

You might also like