Functions:
Functions are special commands that perform computations of processes on a
column, string constant, value or group of values, they can be used anywhere an
expression or condition is needed within the full sql * plus command set.
Types of functions:
Character functions
Number functions
Date functions
Aggregate functions
Conversion functions
Other functions
Dual
Oracle provides a special table named, DUAL, which can be used to test any
function. It is a small oracle worktable, which consists of only one row and one
column, and contains the value x in that column.
Character Functions:
Character functions are used primarily to modify character columns.
LOWER: It converts strings to all lowercase.
Syntax : LOWER (‘string’);
Example: SELECT LOWER (‘RAMESH’) FROM DUAL;
Output : ramesh
UPPER : It converts strings to all uppercase.
Syntax : UPPER (‘string’);
Example : SELECT UPPER (‘ramesh’) FROM DUAL;
Output : RAMESH
INITCAP: It returns string with the first letter in upper case.
Syntax : INITCAP (‘string’);
Example : SELECT INITCAP (‘RAMESH KUMAR’) FROM DUAL;
Output : Ramesh Kumar
LENGTH : It returns the length of string.
Syntax : LENGTH (‘string’);
Example : SELECT LENGTH (‘RAMESH’) FROM DUAL;
Output : 6
INSTR: It will find the location of string x in the string data
Syntax : INSTR (‘string1’,’string2’);
Example : SELECT INSTR (‘MANIPAL UNIVERSITY’,’PAL’)
FROM DUAL;
Output : 5
SUBSTR: It returns a portion of character, beginning at character m,n characters
long (if n is omitted, to the end character). The first position of character is 1.
Syntax : SUBSTR (‘character’, m,[n]);
Example : SELECT SUBSTR (‘MANIPAL UNIVERSITY’,5,7)
FROM DUAL;
Output : PAL
RPAD: It returns character1, right-padded to length n with the characters in
character2, replicated as many times as necessary, if character2 is omitted, right-
pad with blanks.
Syntax : RPAD (‘character1’,n,’character2’);
Example : SELECT RPAD (‘RAMESH’,10,’*’) FROM DUAL;
Output : RAMESH****
LPAD: It returns character1, left-padded to length n with the sequence of
characters in character2, character2 defaults to blanks.
Syntax : LPAD (‘character1’,n,’character2’);
Example : SELECT RPAD (‘RAMESH’,10,’*’) FROM DUAL;
Output : ****RAMESH
LTRIM: It removes characters from the left of character with initial characters
removed upto the first character not in set.
Syntax : LTRIM (‘string’,’set’);
Example : SELECT LTRIM (‘xxxXxxRAMESH’,’x’) FROM DUAL;
Output : XxxRAMESH
RTRIM: It returns character, with final characters removed after the last character
not in the set. Set is optional
Syntax : RTRIM (‘character’,’set’);
Example : SELECT RTRIM (‘RAMESHxxXxx’,’x’) FROM DUAL;
Output : RAMESHxxX
CHR : It convert ASCII values into characters.
Syntax : CHR (string);
Example : SELECT CHR (169) FROM DUAL;
Output : A
ASCII : It converts characters into ASCII values.
Syntax : ASCII (string);
Example : SELECT ASCII (‘A’) FROM DUAL;
Output : 169
TRANSLATE : It translates character set in from_c to character set n to_c.
Syntax : TRANSLATE (‘character’,’from_c’,’to_c’);
Example : SELECT TRANSLATE (‘MUMBAI’,’MU’,’AU’) FROM
DUAL;
Output : ACMBAI
NUMBER FUNCTIONS:
ABS : It returns the absolute value of data (i.e. positive
value)
Syntax : ABS (data);
Example : SELECT ABS (-5000) FROM DUAL;
Output : 5000
CEIL : It returns the smallest integer greater than or
equal to data.
Syntax : CEIL (data);
Example : SELECT CEIL (123.55) FROM DUAL;
Output : 124
FLOOR : It returns the largest integer less than or equal to
data.
Syntax : FLOOR (data);
Example : SELECT FLOOR (123.55) FROM DUAL;
Output : 123
MOD : It returns the modulus of dividing data by y.
Syntax : MOD (data,y);
Example : SELECT MOD (101,2) FROM DUAL;
Output : 1
POWER : It returns the data raised to the power of y.
Syntax : POWER (data,y);
Example : SELECT POWER (10,2) FROM DUAL;
Output : 100
ROUND : Where n is the number of decimal places to
which the data is rounded.
Syntax : ROUND (data,n);
Example : SELECT ROUND (123.55,0), ROUND(123.55,1)
FROM DUAL;
Output : 124 123.6
SQRT : It returns the square root of data.
Syntax : SQRT (data);
Example : SELECT SQRT (64) FROM DUAL;
Output : 8
TRUNC : Where n is the number of decimal places for
truncation
Syntax : TRUNC (data,n);
Example : SELECT TRUNC (123.55,0) TRUNC(123.55,1)
FROM DUAL;
Output : 123 123.5
AGGREGATE FUNCTIONS:
AVG : It returns average value of n, ignoring null values
Syntax : AVG ([DISTINCT|ALL] n);
Example : SELECT AVG (rate) FROM ITEM_MASTER;
MIN : It returns minimum value of expression.
Syntax : MIN ([DISTINCT|ALL] expr);
Example : SELECT MIN (marks) FROM STUDENT;
MAX : It returns maximum value of expression.
Syntax : MAX ([DISTINCT|ALL] expr);
Example : SELECT MAX (marks) FROM STUDENT;
COUNT : It returns the number of rows where expression is
not null.
Syntax : COUNT ([DISTINCT|ALL] expr);
Example : SELECT COUNT (roll) FROM STUDENT;
COUNT(*) : It returns the number of rows in the table including duplicates and
those with null.
Syntax : COUNT (*);
Example : SELECT COUNT (*) FROM STUDENT;
SUM : It returns sum of values of n.
Syntax : SUM ([DISTINCT|ALL] expr);
Example : SELECT SUM (qty) FROM ITEM_MASTER;
DATE FUNCTION:
Consider the date 15-Jan-98
ADD_MONTHS : To add months to one date to get another.
Syntax : ADD_MONTHS (date,value);
Example : SELECT ADD_MONTHS (’15-JAN-98’,5) FROM
DUAL;
Output : 15-JAN-98
LAST_DAY : The last day of a date’s month.
Syntax : LAST_DAY (date);
Example : SELECT LAST_DAY (’15-JAN-98’,5) FROM DUAL;
Output : 31-JAN-98
Example : SELECT LAST_DAY (’15-FEB-98’,5) FROM DUAL;
Output : 28-FEB-98
MONTHS_BETWEEN : Number of months between 2 dates.
Syntax : MONTHS_BETWEEN (date1,date2);
Example : SELECT MONTHS_BETWEEN (’15-JAN-
98’,’20- MAR-98’) FROM DUAL;
NEXT_DAY : Day of next occurrences of a week
Syntax : NEXT_DAY(date1,day of week);
Example : SELECT NEXT_DAY (’15-JAN-98’,’MON’) FROM
DUAL;
Output : 19-JAN-98
TO display system date in SQL
Example : SELECT SYSDATE FROM DUAL;
Output : 18-AUG-2007
TO display User name to work
Example : SELECT USER FROM DUAL;
Output : SCOTT
TO display system time in SQL
Example : SELECT TO_CHAR (SYSDATE,’HH:MI:SS’)FROM
DUAL;
Output : 06:37:30
CONVERSION FUNCTIONS:
TO_CHAR: TO_CHAR function can be used to convert a date
or number to a character stiring.
Syntax : TO_CHAR (n[,fmt]);
With the TO_CHAR function, a date item can be formatted many ways. The format
must be enclosed within single quotes. Consider 19-AUG-96 as the system date,
then.
Example : SELECT TO_CHAR (SYSDATE,’MONTH’) FROM
DUAL;
Output : August
Example : SELECT TO_CHAR (SYSDATE,’dd/mm/yyyy’) FROM DUAL;
Output : 19/08/1996
OTHER FUNCTIONS:
NVL : The NVL function is used to substitute any NULL
value with a user specified value.
Syntax : NVL (x,expression);
Example : SELECT EMP_NO,ENAME,NVL(COMM,200) FROM
DUAL;
Q. Write a query in SQL whose sal>1500 then the commission is 200.
Q. Write a query in SQL whose emp_no=1001 then the commission will be null.
DECODE: The DECODE function can be used to expand any abbreviations used in
the table.
Q. PATNA has been coded as PAT, RANCHI as RAN, JAMSHEDPUR a JAM, BOKARO
as BOK, in the column, city of the customer table.
Example : SELECT CITY, DECODE (CITY,’PAT’, ’PATNA’, ’RAN’,
’RANCHI’,’JAM’,’JAMSHEDPU’,’BOK’,’BOKARO’,’NOT SPECIFIED’)”CITY NAME” FROM
CUSTOMER;
GREATEST: Greatest is used to display highest value of a list of values.
Syntax : GREATEST (List of values);
Example : SELECT GREATEST (10,30,20,15) FROM DUAL;
LEAST : Least is used to display lowest value of a list of values.
Syntax : LEAST (List of values);
Example : SELECT LEAST (10,30,-2,20,-3) FROM DUAL;