Functions
Functions
FUNCTIONS
Functions are very powerful feature of SQL used to
manipulate data items .
SQL functions are built into oracle database and are
operated for use in various appropriate SQL
statements.
If you call a SQL function with a null argument, then
the SQL function automatically returns null. The only
SQL functions that do not necessarily follow this
behavior are CONCAT, NVL, REPLACE,
and REGEXP_REPLACE.
Functions are similar to operators in that they
manipulate data items and return a result.
SQL FUNCTION
Function
s
Arg 1 Function performs
action
arg2 Resullt
value
Arg n
ADVANTAGES OF FUNCTION
Function can be used to perform complex
calculations on data.
Functions can modify individual data items
Function can very easily manipulate output
for groups of rows. Function can manipulate
character as well as numeric type of data.
function can alter date formats for display
TYPES OF FUNCTION
There are two types of function:
Single row functions
Multiple row functions
SINGLE ROW FUNCTION
These function operate on single rows only and
return one value for ach row, column name or an
expression. Single-row functions can be used in
SELECT. WHERE and ORDER by clauses.
Syntax of using a single-row function is
function_name [(arg1, arg2,…..)]
DATABASE MANAGEMENT
deccabdddeec
NUMERIC FUNCTIONS…..
• 1. ABS:- returns the absolute value of ‘n’.
syntax:- ABS(-15)
e.g. Select ABS(-15) “absolute” from
dual;
Z
EXAMPLE :
GREATEST(‘HARD’,’HARRY’,’HAROLD’) FROM DUAL;
OUTPUT:
GREAT
-----------
HARRY
Least(exp1,exp2,exp3…)
This function returns the least value in the list of expressions.
LEAST function behaves same like Greatest , in which all expressions
are implicitly converted to the data type of the first.
EXAMPLE:
Select least(44,22,7) from dual;
Output:
Least(44,22,7)
--------------------------
7
AGGREGATE FUNCTIONS.....
ADD_MONTH(d,n)
This function adds or subtract months to or from date, it returns a date as result.
Example:
SELECT SYSDATE, ADD_MONTHS(SYSDATE,4) FROM DUAL;
OUTPUT:
SYSDATE ADD_MONTHS
------------ ------------------
03-APR-13 03-AUG-13
MONTHS_BETWEENd1,d2)
This function returns the number of months between two dates, d1and
d2. if d1 is later than d2, then the result is positive. If d1 is earlier than d2,
then the result is negative. The output will be a number.
Example
SELECT MAONTHS_BETWEEN(“25-DEC-81’,25-DEC-79’) AS DATE1,
MONTHS_BETWEEN(‘25-DEC-79’,’25-DEC-81’) AS DATE2 FROM DUAL;
OUTPUT:
DATE1 DATE2
--------- ----------
24 -24
NEXT_DAY(DATE,DAY)
THIS FUNCTION RETURNS THE DATE OF NEXT SPECIFIED DAY OF THE WEEK AFTER THE
‘DATE’.
EXAMPLE
SELECT SYSDATE, NEXT_DAY(SYSDATE,’FRIDAY) FROM DUAL;
OUTPUT:
SYSDATE NEXT_Day(
------------- ---------------
03-SEP-13 06-SEP-13
LAST_DAY(d)
This function returns the date of the last day of the month specified. The result will
be a date.
Example:
SELECT SYSDATE, LAST_DAY(SYSDATE) FROM DUAL;
OUTPUT:
SYSDATE LAST_DAY(
------------ ---- -------------------
03-SEP-13 30-SEP-13
ROUND(d[,format])
This function rounds the date d to the unit specified by format. If format is not
specified, is default to ‘DD’ , which rounds d to the nearest day.
Example:
SELECT SYSDATE, ROUND(SYSDATE,’MM’) AS “NEAREST MONTH” FROM DUAL;
OUTPUT:
SYSDATE NEAREST M
-------------- ---------------
03-SEP-13 01-SEP-13
TRUNC(d[,formt ])
This function returns the date d truncated to the
unit specified by format. If format is omitted, then it
defaults to ‘DD’, which truncates d to the nearest day.
Example:
SELECT SYSDATE, TRUNC(SYSDATE,’YEAR’) AS “FIRST DAY” FROM DUAL;
OUTPUT:
SYSDATE FIRST DAY
--------------- ------------------
03-SEP-13 01-JAN-13