You are on page 1of 5

SQL FUNCTIONS

1. DATE FUNCTIONS

I. ADD_MONTHS –Returns a date after adding a specified date with the


specified number of months.

Syntax: add_months (d, n)

Example: select add_months ('07-Feb-23', 2) from dual;

Output:
ADD_MONTH
------------------
07-APR-23

II. LAST_DAY – Returns the date corresponding to the last day of the month.

Syntax: last_day (d)

Example: select sysdate, last_day (sysdate) from dual;

Output:
SYSDATE LAST_DAY
------------- --------------
07-FEB-23 28-FEB-23

III. MONTHS_BETWEEN – Returns the number of months between two dates.

Syntax: months_between (d1, d2)

Example: select months_between ('01-Apr-23', '01-Feb-23') from dual;

Output:
MONTHS_BETWEEN ('01-APR-23', '01-FEB-23')
--------------------------------------------------------------
2

IV. NEXT_DAY – Returns the next date for the specified day from the specified date.

Syntax: next_day (d, day)

Example: select next_day ('07-Feb-23', 'Thursday') from dual;


 The Thursday that immediately follows the date '07-Feb-23' will be displayed.

Output:

NEXT_DAY
------------------
14-FEB-23

V. GREATEST – Returns the latest date present in the argument.

Syntax: greatest (d1, d2, …)

Example: select greatest (to_date ('07-Feb-23', 'dd-Mon-yy'), to_date ('10-Jul-23',


'dd-Mon-yy'), to_date ('15-Jun-23', 'dd-Mon-yy')) from dual;
Output:

GREATEST
-----------------
10-JUL-23

2. CHARACTER FUNCTIONS /STRING FUNTIONS

FUNCTION INPUT OUTPUT

Initcap (char) select initcap (‘hello’) from dual; Hello

Lower (char) select lower (‘FUN’) from dual; fun

Upper (char) select upper (‘sun’) from dual; SUN

Ltrim (char, set) select ltrim (‘xyzadams’, ‘xyz’) from dual; adams

Rtrim (char, set) select rtrim (‘xyzadams’, ‘ams’) from dual; xyzad

Translate (char, from, to) select translate (‘jack’, ‘j’, ‘b’) from dual; back

Replace (char, select replace (‘jack and jue’, ‘j’, ‘bl’) from
black and blue
searchstring, [rep string]) dual;

Substr (char, m, n) select substr (‘abcdefg’, 3, 2) from dual; cd


I. LPAD

Example: select lpad ('function', 15, '=') from dual;

 The output gives the sign ‘=’ before the word function. The entire string is 15 in
length after the padding is done.

Output:

LPAD ('FUNCTION'
--------------------------
= = = = = = =function

II. RPAD – Pads the value given to the right of the given string.

Example: select rpad ('function', 15, '=') from dual;

Output:

RPAD ('FUNCTION'
--------------------------
Function= = = = = = =

3. NUMERIC FUNCTIONS

FUNCTION INPUT OUTPUT


Abs select abs (–15) from dual; 15
select ceil (44.778) from dual; 45
Ceil (n)
select ceil (44.478) from dual; 45
Cos (n) select cos (180) from dual; –.59846007
Exp (n) select exp (4) from dual; 54.59815
Floor (n) select floor (100.75) from dual; 100
Power (m, n) select power (4, 2) from dual; 16
Mod (m, n) select mod (10, 3) from dual; 1
Round (m, n) select round (100.256, 2) from dual; 100.26
Trunc (m, n) select trunc (100.256, 2) from dual; 100.25
Sqrt (n) select sqrt (4) from dual; 2

FLOOR() function rounds the number, towards zero, always down. 


CEILING() function rounds the number, away from zero, always up.

ROUND() function rounds a number to a specified number of decimal places.

You might also like