Professional Documents
Culture Documents
Arithmetic Functions
Arithmetic Functions
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);
2. FLOOR():- This function is used to get the smallest integer value, which is less
than or equal to the given numeric expression.
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.
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;
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
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
--Ex:
--Ex:
--3) current_timestamp: it returns the current timestamp with active time zone
information of server/database.
--Ex:
--4) systimestamp: this will returns the sysdate , including fractional seconds and
time zone of the system.
--5). local timestamp: this will return local timestamp in the active time zone
information with no time zone
-- information shown.
--6). Add_months: it is used to 'add or remove no of moths' from the specified date
--Syntax:
Add_months(DATE,no.of MONTHS)
--Syntax:
Months_between('Date1','Date2')
SELECT months_between(to_date('12/01/2020','dd/mm/yyyy'),to_date('24/02/2019','dd/
mm/yyyy'))"Months" FROM dual;
-- 8). Next_day: it returns 'next day date' of the given day from specified date.
--Syntax:
next_day('DATE','Day')
--Syntax:
last_day ('DATE');
--Syntax:
EXTRACT(YEAR/MONTH/DAY/HOUR/MINUTE FROM DATE);
--Ex:
SELECT to_char(SYSDATE,'DAY/MONTH/YEAR') FROM dual;
-- Q) display the employee who are joined in December month from employees table?
--12) To_date: it is used to convert string date into oracle date format.
--Syntax:
GREATEST(date1,date2,date3);
--Ex:
--Ex:
--15) Round: it rounds the date to which it was equal to or greater than the given
date.
--Syntax:
ROUND(DATE,(DAY/MONTH/YEAR));
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.
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;
-- Syntax:
TRUNC(DATE,(DAY/MONTH/YEAR));
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.
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;
--17) New_time: this will give the desired timezone's date and time.
-- Syntax:
new_time(DATE,current_timezone,desired_timezone);
--TIMEZONES:
-- Ex:
SELECT to_char(new_time(SYSDATE,'GMT','YST'),'DD/MON/YYYY HH:MI:SS AM') FROM dual;
--Ex:
SELECT COALESCE('12/jan/2010','13/jan/2010'),COALESCE(NULL,'7/feb/2010','28/march/
2009',NULL) FROM dual;
-----------------------------------------------------------------------------------
------------------------------------------------------------------
--5) Conversion Function
--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.