Вы находитесь на странице: 1из 9

Single-Row Functions

Single-row functions return a single result row for every row of a queried table or view. These functions can
appear in select lists, WHERE clauses, START WITH andCONNECT BY clauses, and HAVING clauses

Numeric Functions
Numeric functions accept numeric input and return numeric values. Most numeric functions
return NUMBER values that are accurate to 38 decimal digits.

ABS
Syntax

Purpose
ABS returns the absolute value of n.

SELECT ABS(-15) "Absolute"

FROM DUAL;

Absolute

----------

15

CEIL
Syntax
select ceil(-3.3) from dual select ceil(-3.6) from dual select ceil(3.3) from dual select ceil(3.6) from dual
3 3 4 4

Rounded to nearest largest integer

FLOOR
Syntax

select floor(3.6)
select floor(-3.3) from dual select floor(-3.6) from dual select floor(3.3) from dual
from dual
-4 -4 3 3

Rounded to nearest smaller integer

MOD
Syntax

Purpose
MOD returns the remainder of n2 divided by n1. Returns n2 if n1 is 0.

SELECT MOD(11,4) "Modulus"

FROM DUAL;

ROUND (number)
Syntax

round_number::=
If integer is negative result is 0 either n is positive or negative

round(1.4) round(1.5) round(-1.4) round(-1.5)


1 2 -1 -2
consider this as 1.43.4 based
round(1.434,2) round(1.435,2) round(-1.434,2) round(-1.435,2)
upon 4
1.43 1.44 -1.43 -1.44
When negative
ROUND(n, integer) returns -
when integer is 0 or <o then result is zero ROUND(-n, integer).

TRUNC (number)
Syntax

trunc_number::=

Purpose
The TRUNC (number) function returns n1 truncated to n2 decimal places. If n2 is omitted, then n1 is
truncated to 0 places. n2 can be negative to truncate (make zero) n2 digits left of the decimal point.

trunc(1.4) trunc(1.5) trunc(-1.4) trunc(-1.5)


1 1 -1 -1
trunc(1.434,2) trunc(1.435,2) trunc(-1.434,2) trunc(-1.435,2)
1.43 1.43 -1.43 -1.43
when n2<0 then result is zero
Character Functions Returning Character Values

CONCAT
Syntax

SELECT CONCAT(CONCAT(last_name, '''s job category is '), job_id) "Job"

FROM employees

WHERE employee_id = 152;

Job

------------------------------------------------------

Hall's job category is SA_REP

INITCAP
Syntax

SELECT INITCAP('the soap') "Capitals" FROM DUAL;

Capitals

---------

The Soap

LOWER
Syntax
LPAD
Syntax

Purpose

LPAD returns expr1, left-padded to length n characters with the sequence of characters in expr2. This
function is useful for formatting the output of a query.

lpad('hello',10) If you do not specify expr2, then the default is a


hello single blank
lpad('hello',10.'@')
@@@@@hello

RPAD
Syntax

lpad('hello',10) If you do not specify expr2, then the default is a


hello single blank
lpad('hello',10.'@')
hello@@@@@

LTRIM
Syntax

Ltrim('******hello','*')
hello
If you do not specify set,
Ltrim(' hello')
then it defaults to a single
hello blank.

RTRIM
Syntax

Rtrim('hello******','*')
hello
If you do not specify set,
Rtrim('hello ')
then it defaults to a single
hello blank.

TRIM
Syntax

Purpose

TRIM enables you to trim leading or trailing characters (or both) from a character string.
If trim_character or trim_source is a character literal, then you must enclose it in single quotation
marks.
 If you specify LEADING, then Oracle Database removes any leading characters equal to trim_character.
 If you specify TRAILING, then Oracle removes any trailing characters equal to trim_character.
 If you specify BOTH or none of the three, then Oracle removes leading and trailing characters equal
to trim_character.
 If you do not specify trim_character, then the default value is a blank space.
 If you specify only trim_source, then Oracle removes leading and trailing blank spaces.
 The function returns a value with data type VARCHAR2. The maximum length of the value is the length
of trim_source.
 If either trim_source or trim_character is null, then the TRIM function returns null

TRIM(LEADING '0' FROM


TRIM(' tech ') RIM('*' FROM '***tech****')
'000123')
Result: 'tech' Result: 'tech' Result: '123'
TRIM(TRAILING '1' FROM TRIM(BOTH '1' FROM
'Tech1') '123Tech111')
Result: 'Tech' Result: '23Tech'

REPLACE
Syntax

Purpose

REPLACE returns char with every occurrence of search_string replaced with replacement_string.
If replacement_string is omitted or null, then all occurrences of search_string are removed.
If search_string is null, then char is returned

replace('****hello***students****','*') replace('****hello***students****','*','$')
hellostudents $$$$hello$$$students$$$$

SUBSTR
Syntax
substr::=

 If position is 0, then it is treated as 1.


 If position is positive, then Oracle Database counts from the beginning of char to find the first character.
 If position is negative, then Oracle counts backward from the end of char.
 If substring_length is omitted, then Oracle returns all characters to the end of char.
If substring_length is less than 1, then Oracle returns null.

select substr('hello',0) from dual select substr('hello',-1,1) from dual select substr('hello',-1,-1) from dual
hello o null

Character Functions Returning Number Values

LENGTH
Syntax

length::=

SELECT LENGTH('CANDIDE') "Length in characters" FROM DUAL;

Length in character

7
INSTR functions
The INSTR functions (INSTR, INSTRB, INSTRC, INSTR2, and INSTR4) searches a string for a substring
using characters and returns the position in the string that is the first character of a specified occurrence of
the substring. The functions vary in how they determine the position of the substring to return.

Syntax

position is an nonzero integer indicating the character of string where Oracle Database begins the
search.

If position is negative, then Oracle counts backward from the end of string and then searches
backward from the resulting position.

occurrence is an integer indicating which occurrence of string Oracle should search for. The value
of occurrence must be positive.

Default for position and occurrence is 1

select instr('helloeee','e') from select instr('helloeee','e',-1,2) from select instr('helloeee','e',0,3) from


dual dual dual
2 7 0

Datetime Functions
Datetime functions operate on date (DATE), timestamp (TIMESTAMP, TIMESTAMP WITH TIME ZONE,
and TIMESTAMP WITH LOCAL TIME ZONE), and interval
(INTERVAL DAY TO SECOND, INTERVAL YEAR TO MONTH) values.

Вам также может понравиться