Sie sind auf Seite 1von 7

Functions:

SQL*Plus provides specialized functions to perform operations using the DML


Commands. A Function takes one or more arguments and returns a value.
One can broadly classify functions into
1. Single Row Functions
2. Multi Row Functions

Single Row Functions:


A single row function or a scalar function returns only one value for every
row queried in the table. Single row functions can appear in select command and can
also be included in a �where� clause.

� Single row functions:

Character functions
� Lower
� Upper
� ASCII
� CHR
� INITCAP
� LENGTH
� LTRIM
� RTRIM
� TRIM
� RPAD
� LPAD
� SOUNDEX
� CONCAT
� INSTR
� SUBSTR
� ||

Queries Using Character Functions:

Lower: String of characters converted to lower case.

Select dname, lower (dname)


From dept;

Upper: String of characters converted to Upper case.

Select ename, upper (ename)


From EMP;

Concat: To Concat two strings.

Select dname || loc


From dept;

Select dname ||','|| loc


From dept;

Lpad and Rpad: To �pad� the right or left side of a column with any set of
characters. The character set can be anything: spaces, periods, commas, letters or
numbers, pound signs (#), or even exclamation marks (!).

Syntax: RPAD (string, length, [,�set�])


LPAD (string, length, [,�set�])
Select rpad (ename, 20, '.'), sal
From EMP;

Select lpad (ename, 8), sal


From EMP;

Lower and Upper and Initcap:

Select lower (ename), upper (ename), initcap (ename)


From EMP;

Length: Length tells how long a string is i.e. how many characters it has in it,
including letters, spaces, and anything else.

Select ename, length (ename)


From EMP;

Substr: To clip out a piece of a string.

Syntax: SUBSTR (string, start [, count])

Select substr (ename, 2, 4)


From EMP;

Instr: The INSTR function allows for simple or sophisticated searching through a
string for a set of characters

Syntax: INSTR (string, set [, start [, occurrence]])

Select ename, instr (ename, �A�, 1, 1)


From EMP;

Select ename, instr (ename, 'S')


From EMP;

CHR and ASCII: The ASCII and CHR functions are used during ad hoc queries. CHR
converts numeric values to their ASCII character String equivalents.

Select CHR (65)|| CHR (80)|| CHR (80)|| CHR (76)|| CHR (69)
from DUAL;

Select ASCII (�APPLE�) from DUAL

SOUNDEX: This is a one string function that is used almost exclusively in WHERE
clause. It has the unusual ability to find words that sound like other
Words, regardless of how either is spelled.

Syntax: SOUNDEX (string)

Select ENAME, EMPNO, SAL from EMP


Where SOUNDEX (ENAME)= SOUNDEX (�TURNAR�);

Date functions
� SYSDATE
� CURRENT_DATE
� SYSTIMESTAMP
� ADD_MONTHS (date, count)
� CURRENT_TIMESTAMP
� GREATEST (date1, date2, date3�)
� LEAST (date1, date2, date3�)
� LAST_DAY (date)
� MONTHS_BETWEEN (date2, date1)
� NEXT_DAY (date, �day�)
� TO_CHAR (date, �format�)
� TO_TIMESTAMP (�value�)

Queries On Date Functions:

SYSDATE: Select SYSDATE from DUAL

DUAL: DUAL is a small but useful Oracle table created for testing
Functions or doing quick calculations.

CURRENT_DATE: Select CURRENT_DATE from DUAL

SYSTIMESTAMP: Select SYSTIMESTAMP from DUAL

ADD_MONTHS: Select ADD_MONTHS (HIREDATE, 5) as Incdate


From EMP where ENAME=�SMITH�;

CURRENT_TIMESTAMP: Select CURRENT_TIMESTAMP from DUAL

GREATEST: SELECT GREATEST (SYSDATE, '01-OCT-2004') FROM


DUAL;

LEAST: SELECT LEAST (SYSDATE, '02-MAY-2004') FROM DUAL

NEXT_DAY: SELECT NEXT_DAY (SYSDATE, 'FRIDAY') FROM


DUAL;

LAST_DAY: SELECT LAST_DAY (SYSDATE) AS END_MONTH


FROM DUAL

MONTHS_BETWEEN: SELECT MONTHS_BETWEEN (SYSDATE,


'02-MAY-1981')/12 AS AGE FROM DUAL;

TO_CHAR: SELECT HIREDATE, TO_CHAR (HIREDATE,


'MM/DD/YY') AS FORMATTED FROM EMP;

� SELECT TO_CHAR (HIREDATE, 'MONTH, DDth, YEAR') AS


FORMATTED FROM EMP

ERROR:

SELECT TO_CHAR (SYSDATE, ' HH: MI: SS') AS NOW FROM


DUAL;

SELECT TO_CHAR (SYSDATE, 'HH: MM: SS') AS NOW FROM


DUAL;

Data types of Oracle 9i


1. TIMESTAMP: Store date to billionth of second, like
DATE data type it stores the year, month, day, hour, minute, second and also
include fractional seconds
Precision.
2. TIMESTAMP WITH [LOCAL] TIME ZONE:

3. INTERVAL YEAR TO MONTH


4. INTERVAL DAY TO SECOND

Interval year to month and Interval day to second data types are mostly used
During statically analysis and data mining.

E.g. create table new


(Stamp TIMESTAMP (5));

Create table new1


(Stamp TIMESTAMP (5) WITH TIME ZONE);

Create table new1


(Stamp TIMESTAMP (5) WITH LOCAL TIME ZONE);

� Numeric functions
� ABS
� CEIL
� FLOOR
� POWER
� ROUND
� TRUNC
� SQRT
� SIGN
� NVL

Queries on Numeric Functions:

ABS: Absolute value is measure of the magnitude of


Something. Absolute value is always positive.

Syntax: ABS (value)

Select ABS (-30) from DUAL

CEIL: CEIL (for ceiling) simply produces the smallest integer


(or whole number) that is greater than or equal to a specific
Value.

Select CEIL (1.3) from DUAL


Select CEIL (-2.3) from DUAL

FLOOR: FLOOR is the intuitive opposite have CEIL.

Select FLOOR (1.3) from DUAL


Select FLOOR (-2.3) from DUAL

POWER: Syntax: POWER (value, exponent)

Select POWER (4,2) from DUAL

SQRT: This function equivalent to POWER (value, .5).


Select SQRT (64) from DUAL

ROUND and TRUNC: These two are related single-value


Functions.

Syntax: ROUND (value, precision)


TRUNC (value, precision)

Select ROUND (64.1235,3) from DUAL


Select TRUNC (35.2104,2) from DUAL

SIGN: Syntax: SIGN (value)

Select SIGN (-13) from DUAL


Select SIGN (34) from DUAL

NVL: Syntax: NVL (column name, replace value)

Select ename, comm, NVL (comm, 0) as new From EMP;

2. Multi Row Functions:

� Group Value Functions


� AVG
� COUNT
� MAX
� MIN
� SUM
select
from
where
order by
group by
having

ORDER BY - arranging the rows in alphabetical manner - ascending or descending


order
select * from emp
order by empno

select * from emp


order by empno desc

select * from emp


where deptno in (10,20)
order by deptno,job desc,ename

GROUP ING FUNCTIONS

SUM
AVG
COUNT
MAX
MIN

SELECT SUM(SAL)
FROM EMP;

SELECT AVG(SAL),SUM(COMM)
FROM EMP;

SELECT MAX(SAL),SUM(COMM),MIN(SAL)
FROM EMP;

SELECT COUNT(*),COUNT(EMPNO),COUNT(COMM)
FROM EMP;

SELECT DEPTNO,SUM(SAL),MAX(SAL),MIN(SAL)
FROM EMP
GROUP BY DEPTNO

SELECT JOB,SUM(SAL),MAX(SAL),MIN(SAL)
FROM EMP
GROUP BY JOB
HAVING JOB IN ('CLERK','ANALYST')

Dual:
it is the default table provided by the oracle to the user where the user
can check the functions can select all psedo cols where it has one row and one
column called dummy numeric functions
sum abs(-200)
sin() cos()
tan()
select round(89.8789) from dual;
select round(89.8789,2) from dual;
select trunc(89.879) from dual;

character functions

upper()
lower()

select lower(ename) from emp concat()


select concat('raj','esh') from dual;
length(ename) replace('computer','com','cell')
lpad(ename,10,'+')
rpad(ename,10,'+')
select trim(ename) from emp;

Date functions:
To char (sysdate,'day') where the user can retrieve in user defined ways
To char (sysdate,'dd')
To char (sysdate,'dd/month/yyyy')
To char (sysdate,'dd/mon/yy')
To char (sysdate,'ddth-mm-yy')
Select to char(hiredate , 'ddth\yyyy\month') from emp to_date

Das könnte Ihnen auch gefallen