Site search

Categories

Archives

Tags





The GNU/Linux Advanced Administration

Free Linux Manuals !


A Newbie's Getting Started Guide to Linux

-- FREE --
IT Magazine
Subscriptions

Oracle Magazine Oracle Magazine Contains technology strategy articles, sample code, tips, Oracle and partner news, how-to articles for developers and DBAs

WebSite Magazine WebSite Magazine Practical advice, helpful tools and insights for website owners

Dr Dobb's Journal Dr Dobb's Journal enables coders to write the most efficient programs and help in daily programming quandaries

DM Review DM Review is recognized as the premier business intelligence, analytics and data warehousing publication
Various other Free IT magazine subscriptions
NoAdware Free Trial

NoAdware Remove
harmful
adware,
spyware,
trojans,
dialers
and worms!
- Featured ebook -

Database Normalization
by Alf Pedersen

Database Normalization ebook Understand and master how to normalize a database using methods richly documented with graphical ERD and server diagram examples

RSS
XML RSS
What is this?
AddThis Feed Button

Social
Bookmarking


Oracle Functions

Oracle functions are very handy utilities to help you massage a result set to conform to your requirements, whether you are selecting, inserting, deleting or updating.

Below is a list of some commonly used sql functions, with an example of each:


add_months

Returns the supplied date with the requested number of months added to it.
If the supplied date has a day value which is larger than the maximum number of days
in the resulting month, the result is the last day of that month.

SQL> select sysdate from dual;

SYSDATE
——————–
25-dec-2007 23:41:50

SQL> select add_months(sysdate,1) “result” from dual;

result
——————–
25-jan-2008 23:41:52

top of page


avg

Returns the average of a series of numbers.

SQL> select salary from employees;

SALARY
———-
20000
30000
30000
10000

SQL> select avg(salary) “result” from employees;

result
———–
22500

top of page


ceil

Returns the smallest integer larger than the supplied argument.
Opposite of sql function: floor.

SQL> select 99.5, ceil(99.5) “result” from dual;

99.5 result
—- ———-
99.5 100

top of page


chr

Returns the character equivalent of the supplied integer.

SQL> select chr(79)||chr(82)||chr(65)||chr(67)||chr(76)||chr(69) “result” from dual;

result
——
ORACLE

top of page


concat

Returns the first argument concatenated with the second argument.
The result’s dataype will depend on the datatype of the arguments.
I prefer to use the two pipe symbols (||) to concatenate.

SQL> select concat(‘data’,'base’) “result” from dual;

result
——–
database

SQL> select ‘data’||’base’ “result” from dual;

result
——–
database

top of page


count

Returns the number of rows as requested by the query, either all rows or only distinct values.
By specifying *, it will return all rows including duplicates and nulls.
If no rows match your query, count will return 0. (never null)

SQL> select count(*) “result” from emp;

result
———-
14

SQL> select job “result” from emp;

result
———
CLERK
SALESMAN
SALESMAN
MANAGER
SALESMAN
MANAGER
MANAGER
ANALYST
PRESIDENT
SALESMAN
CLERK
CLERK
ANALYST
CLERK

14 rows selected.

SQL> select count(distinct job) “result” from emp ;

result
———-
5

SQL> select count(*) “result” from emp where job = ‘VP’;

result
———-
0

top of page


decode

The oracle decode function takes up to 255 argument sets and returns the relevant result depending on the value found.
This is similar to the ‘case’ statement in other languages.
The last argument is what will be returned if no match is found in the decode statement.

SQL> select deptno “dn” , decode(deptno,10,’ten’,20,’twenty’,30,’thirty’,'other’) “result” from dept;

dn result
– ——
10 ten
20 twenty
30 thirty
40 other

top of page


dump

Returns a string representing the argument’s datatype code, byte length and internal representation.
Very handy command if you are hunting for unexpected or hidden characters in your data.

SQL> select dump(‘cat’) “result” from dual;

result
———————-
Typ=96 Len=3: 99,97,116

SQL> select dump(‘cat ‘) “result” from dual;

result
————————-
Typ=96 Len=4: 99,97,116,32

top of page


floor

Returns an integer equal to or 1 less than the argument.
Opposite of oracle function : ceil.

SQL> select 99.5, floor(99.5) “result” from dual;

99.5 result
—- ———-
99.5 99

top of page


greatest

Returns greatest ranking argument out of a list of arguments.
The ranking is determined by the value of the numerical codes of the whole character string.
Opposite of oracle function : least.

SQL> select greatest(‘oracle’,'oracle10G’) “result” from dual;

result
———
oracle10G

top of page


initcap

Returns the string with each word’s first letter capitalized.
Similar to oracle functions : lower and upper

SQL> select initcap(‘oracle database tips’) “result” from dual;

result
——————–
Oracle Database Tips

top of page


instr

Returns the first position of a string inside another string.
The 1st argument is the string to be searched.
The 2nd argument specifies the character or string to search for.
The 3rd argument allows you to specify from which character position to start searching.
The 4th argument allows you to specify which occurence of the search string to return.

SQL> select instr(‘package’,'a’) “result” from dual;

result
——
2

SQL> select instr(‘package’,'a’,3,1) “result” from dual;

result
——
5

SQL> select instr(‘package’,'a’,1,2) “result” from dual;

result
———-

5

top of page


last_day

Returns the last day’s date of the supplied date’s month.

SQL> select sysdate, last_day(sysdate) “result ” from dual;

SYSDATE result
——————– ——————–
27-dec-2007 15:49:27 31-dec-2007 15:49:27

top of page


least

Returns the least or smallest ranking argument out of a list of arguments.
The ranking is determined by the value of the numerical codes of the whole character string.
Opposite of oracle function : greatest

SQL> select least(‘oracle’,'oracle10G’) “result” from dual;

result
——
oracle

top of page


length

Returns the length (as an integer) of the supplied argument.

SQL> select length(‘oracle database’) “result” from dual;

result
———-
15

top of page


lower

Returns the argument in lower case.
Similar to oracle functions : upper and initcap

SQL> select lower(‘ORACLE’) “result” from dual;

result
——
oracle

top of page


lpad

Returns the first argument, left-padded by the third argument, so that the complete string is as long as specified by the second argument.
If you do not supply the third argument, blank spaces are used for padding.
Similar to oracle function : rpad

SQL> select lpad(‘oracle’,10,’*') “result” from dual;

result
———-
****oracle

SQL> select lpad(‘oracle’,10) “result” from dual;

result
———-
oracle

top of page


ltrim

Removes the second argument from the first argument.
If you do not supply the second argument, it will remove blank spaces.
Similar to oracle function: rtrim

SQL> select ltrim(‘oracle database’,'orac’) “result” from dual;

result
———–
le database

SQL> select ltrim(‘ oracle’) “result” from dual;

result
——
oracle

top of page


max

Returns the largest of a series of numbers.
Similar to oracle function : min

SQL> select grade from salgrade;

GRADE
———-
1
2
3
4
5

SQL> select max(grade) “result” from salgrade;

result
———-
5

top of page


min

Returns the smallest in a series of numbers.
Similar to oracle function : max

SQL> select grade from salgrade;

GRADE
———-
1
2
3
4
5

SQL> select min(grade) “result” from salgrade;

result
———-
1

top of page


mod

Returns the remainder of the first argument divided by the second argument.
The result is floor-ed.

SQL> select mod(222,23) “result” from dual;

result
———-
15


months_between

This date-oriented oracle function returns the number of months between two dates as a fraction.

SQL> select months_between(sysdate+320 ,sysdate) “result” from dual;

result
———-
10.483871

top of page


next_day

Returns date of the day of the week (supplied as the second argument) after the date of the first argument.
The second argument is specified as the name of a weekday.

SQL> select next_day(’01-JAN-2008′,’SUNDAY’) “result” from dual;

result
——————–
06-jan-2008 00:00:00

top of page


nullif

Returns null if the first and second argument are equal, if they are unequal, it will return the first argument.

SQL> select nullif(1,1) “result” from dual;

result
———-

SQL> select nullif(1,2) “result” from dual;

result
———-
1

top of page


nvl

Returns the second argument if the first argument is null.
Otherwise, the first argument is returned.
Similar to oracle function : nvl2

SQL> select nvl(null,’no value’) “result” from dual;

result
——–
no value

SQL> select nvl(‘some value’,'no value’) “result” from dual;

result
———-
some value

top of page


nvl2

This function is an extension to nvl by allowing you to specify a value to return if the argument to be checked is null.
So, nvl2 will return the third argument if the first argument is null and will return the second argument if the first argument is not null.
Similar to oracle function : nvl

SQL> select nvl2(null,’has value’,'no value’) “result” from dual;

result
——–
no value

SQL> select nvl2(‘some value’,'has value’,'no value’) “result” from dual;

result
———
has value

top of page


replace

Returns the first argument with every occurence of the second argument replaced with the third argument.
If the third argument is left out, the second argument is removed from the first argument.
If the second argument is null, then the first argument is returned unmodified.

SQL> select replace(‘down in the creek’,'down in’,'up’) “result” from dual;

result
————
up the creek

SQL> select replace(‘down in the creek’,'down in’) “result” from dual;

result
———-
the creek

SQL> select replace(‘down in the creek’,null,’up’) “result” from dual;

result
—————–
down in the creek

top of page


round (number)

Returns the first argument rounded to the number of places after the decimal point as specified by the second argument.
If the second argument is not specified, then the first argument is rounded to 0 places after the decimal point.
If the result is n.5, round will round up to the next integer, if the result is -n.5, it will round down to the next integer.

SQL> select round(8.345,2) “result” from dual;

result
———-
8.35

SQL> select round(8.345) “result” from dual;

result
———-
8

SQL> select round(8.5) “result” from dual;

result
———-
9

SQL> select round(-8.5) “result” from dual;

result
———-
-9

round (date)

This will always return a date.
The format of the second argument determines the timeperiod it will round to, like YEAR, MONTH, DAY, HH and MI.
If you do not specify a second argument, the format defaults to day.

SQL> select sysdate “result” from dual;

result
——————–
04-jan-2008 15:34:19

SQL> select round(sysdate) “result” from dual;

result
——————–
05-jan-2008 00:00:00

SQL> select round(sysdate,’YYYY’) “result” from dual;

result
——————–
01-jan-2008 00:00:00

top of page


rpad

Returns the first argument, right-padded by the third argument, so that the complete string is as long as specified by the second argument.
If you do not supply the third argument, blank spaces are used for padding.
Similar to oracle function : lpad

SQL> select rpad(‘star status ‘,15,’*') “result” from dual;

result
—————
star status ***

top of page


rtrim

Removes the second argument from the first argument.
If you do not supply the second argument, it will remove blank spaces.
Similar to oracle function : ltrim

SQL> select rtrim(‘oracle***’,'*’) “result” from dual;

result
——
oracle

top of page


sqrt

Returns the square root of the argument.

SQL> select sqrt(16) “result” from dual;

result
———-
4

top of page


substr

This very useful sql function returns the part of the first argument, determined by what you specify as the second and third arguments.
The second argument tells Oracle from which character position to start counting.
It can contain a positive or negative value.
The third argument determines the number of characters to select.
If you do not supply this argument, all characters until the end are selected.

Read up more about this very useful oracle function :
Oracle online documentation : SQL Reference, substr

SQL> select substr(‘oracle functions’,8,8) “result” from dual;

result
——-
function

SQL> select substr(‘oracle functions’,8) “result” from dual;

result
——————
functions

top of page


sum

Returns the sum of a range of numbers.

SQL> select sal from emp;

SAL
———-
800
1600
1250
2975
1250
2850
2450
3000
5000
1500
1100
950
3000
1300

14 rows selected.

SQL> select sum(sal) “result” from emp;

result
———-
29025

top of page


sysdate

Returns the current date and time of the local database server’s operating system.
You will see the date format as it is specified by the NLS_DATE_FORMAT
database initialization parameter.
You can set NLS_DATE_FORMAT for your session to overwrite the database’s default value.
Alternatively, you can convert sysdate to a character datatype using the to_char function.

SQL> select sysdate “result” from dual;

result
——————–
05-jan-2008 16:57:11

SQL> alter session set nls_date_format=’dd/mm/yyyy hh24-mi’;

Session altered.

SQL> select sysdate “result” from dual;

result
—————-
05/01/2008 16-58

SQL> select to_char(sysdate,’dd/mon/yyyy hh24:mi:ss’) “result” from dual;

result
——————–
05/jan/2008 16:58:36

top of page


to_char

Converts the argument to a character datatype, using the format specified by the second argument.
You can convert number, date and CLOB datatypes.

Read up more about format models here:
Oracle online documentation : SQL Reference, format models

SQL> select to_char(sal,’L99G999D99MI’) “result” from emp;

result
——————–
$800.00
$1,600.00
$1,250.00
$2,975.00
$1,250.00
$2,850.00
$2,450.00
$3,000.00
$5,000.00
$1,500.00
$1,100.00
$950.00
$3,000.00
$1,300.00

SQL> select to_char(sysdate,’DD-MON-YYYY hh24:mi:ss’) “result” from dual;

result
——————–
05-JAN-2008 16:42:27

select to_char(clob_column) “result” from large_table where itemno = 469;

result
————————————————————————–
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed
diam nonumy eirmod tempor invidunt ut labore et dolore magna
aliquyam erat, sed diam voluptua. At vero eos et accusam et
justo duo dolores et ea rebum. Stet clita kasd gubergren, no
sea takimata sanctus est Lorem ipsum dolor sit amet.

top of page


to_date

Converts a character string to a date datatype.
If the character string is already in the default format, you do not need to specify a format as the second argument.
The default format is specified by initialization parameter NLS_TERRITORY or can be set by NLS_DATE_FORMAT.

Read up more about format models here:
Oracle online documentation : SQL Reference, oracle function format models

SQL> select * from nls_session_parameters where parameter = ‘NLS_DATE_FORMAT’;

PARAMETER VALUE
————————– —————————————-
NLS_DATE_FORMAT dd-mon-yyyy

SQL> select to_date(’01-jan-2008′) “result” from dual;

result
———–
01-jan-2008

SQL> select to_date(’01/01/2008′) “result” from dual;
select to_date(’01/01/2008′) “result” from dual
*
ERROR at line 1:
ORA-01843: not a valid month

SQL> select to_date(’01/01/2008′,’DD/MM/YYYY’) “result” from dual;

result
———–
01-jan-2008

top of page


to_number

Converts a character datatype to a number datatype
The second argument supplies the format needed to do the conversion.

Read up more about format models here:
Oracle online documentation : SQL Reference, format models

SQL> select to_number(‘$800.00′,’L9G999D99′) “result” from dual;

result
———-
800

top of page


translate

Returns a string which replaces all occurences of the characters in the second argument
with the characters of the third argument.
If the second argument contains more characters than the third argument,
the extra characters are removed from the result.

SQL> select translate(‘November’,'Nov’,'Dec’) “result” from dual;

result
——–
December

SQL> select translate(‘September’,'Sept’,'Dec’) “result” from dual;

result
——–
December

top of page


trim

Returns the third argument with the leading, ending or both sides of the argument’s characters trimmed.
The first argument specifies which side to trim from.
Values can be ‘LEADING’,'TRAILING’ or ‘BOTH’.
If null, both sides are trimmed.
The second argument specifies which character to trim. If null, blanks are assumed.
The third argument takes the form : FROM < source > which can be a column name.

SQL> select ename from emp where ename = ‘FORD’;

ENAME
———-
FORD

SQL> select trim(leading ‘F’ from ename) “result” from emp where ename = ‘FORD’;

result
———-
ORD

top of page


trunc(number)

Returns the first argument truncated to the number of decimal places determined by the second argument.
If the second argument is not specified, Oracle assumes 0.

SQL> select trunc(18.456,2) “result” from dual;

result
———-
18.45

SQL> select trunc(18.456) “result” from dual;

result
———-
18


trunc(date)

Returns the date truncated to the day specified by the second argument’s format.
The format of the second argument determines the timeperiod it will truncate to, like YEAR, MONTH, DAY, HH and MI.
If you do not specify a second argument, the format defaults to DAY.

SQL> select sysdate “result” from dual;

result
———–
05-jan-2008

SQL> select trunc(sysdate,’YEAR’) “result” from dual;

result
———–
01-jan-2008

top of page


upper

Returns the argument with all characters in upper case.
Similar to oracle functions : lower and initcap

SQL> select upper(‘oracle functions’) “result” from dual;

result
————————–
ORACLE FUNCTIONS

Recommended reading for more information on oracle functions :

Oracle online documentation: SQL Reference