
- •Laboratory work 5
- •Aggregate functions
- •Single row functions
- •Number functions
- •Character functions
- •Date functions
- •Conversion Functions
- •Miscellaneous Single Row Functions
- •Sgroup by and having clause
- •Syntax:
- •Purpose
- •Order by clause
- •Syntax:
- •Purpose
- •Description and examples
- •Aggregate functions
- •Sgroup by and having clauses
- •Order by clause
- •Варианты заданий
- •Вариант 1
- •Вариант 2
- •Вариант 3
- •Вариант 4
- •Вариант 5
- •Вариант 6
- •Вариант 7
- •Вариант 8
- •Вариант 9
- •Вариант 10
- •Вариант 11
- •Вариант 12
- •Вариант 13
- •Вариант 14
- •Вариант 15
- •Вариант 16
- •Вариант 17
- •Вариант 18
- •Control questions
- •Appendices
- •Appendix a. Answer to lab task
Number functions
Func-tion
Syntax
Purpose
Example
ABS
Returns the absolute value of n.
SELECT ABS(-15) "Absolute"
FROM DUAL;
CEIL
Returns smallest integer greater than or equal to n.
SELECT CEIL(15.7) "Ceil"
FROM DUAL;
FLOOR
Returns largest integer equal to or less than n.
SELECT FLOOR(15.7) "Floor"
FROM DUAL;
SIN, COS, TAN
FUN is a function name.
Return sin, cos or tan of n (an angle expressed in radians).
SELECT SIN(30*3.1415/180)
FROM DUAL;
SINH, COSH, TANH
FUN is a function name.
Returns the hyperbolic sin, cos or tan of n.
SELECT SINH(1) AS
"Hyperbolic sine of 1"
FROM DUAL;
EXP
Returns e raised to the nth power, where e = 2.71828183 ...
SELECT EXP(4) AS
"e to the 4thpower"
FROM DUAL;
LN
Returns the natural logarithm of n, where n is greater than 0.
SELECT LN(95) AS
"Natural log of 95"
FROM DUAL;
LOG
Returns the log base m, of n. The base m can be any positive number other than 0 or 1 and n can be any positive number.
SELECT LOG(10,100) AS
"Log base 10 of 100"
FROM DUAL;
MOD
Returns remainder of m divided by n. Returns m if n is 0.
SELECT MOD(11,4)
FROM DUAL;
POWER
Returns m raised to the nth power. The base m and the exponent n can be any numbers, but if m is negative, n must be an integer.
SELECT POWER(3,2)
FROM DUAL;
SIGN
If n<0, the function returns -1. If n=0, the function returns 0. Ifn>0, the function returns 1.
SELECT SIGN(-15) "Sign"
FROM DUAL;
SQRT
Returns square root of n. The value n cannot be negative. SQRT returns a "real" result.
SELECT SQRT(26)
FROM DUAL;
ROUND
Returns n rounded to m places right of the decimal point. If m is omitted, n is rounded to 0 places. m can be negative to round off digits left of the decimal point. m must be an integer.
SELECT ROUND(15.193,1)
AS "Round"
FROM DUAL;
TRUNC
Returns n truncated to m decimal places. If m is omitted, n is truncated to 0 places. m can be negative to truncate (make zero) m digits left of the decimal point.
SELECT TRUNC(15.79,1)
AS "Truncate"
FROM DUAL;
Character functions
There are two types of character functions:
returning character values and
returning number values.
They are discussed bellow. Some of them are present in simplified variant.
Character functions returning character values
Func-tion |
Syntax |
Purpose |
Example |
CHR |
|
Returns the character having the binary equivalent to n in either the database character set or the national character set. |
SELECT CHR(67) FROM DUAL; |
CONCAT |
|
Returns char1 concatenated with char2. This function is equivalent to the concatenation operator (||). |
SELECT CONCAT('AB','CD') FROM DUAL; |
INITCAP |
|
Returns char, with the first letter of each word in uppercase, all other letters in lowercase. Words are delimited by white space or characters that are not alphanumeric |
SELECT INITCAP('the soap') FROM DUAL; |
LOWER |
|
Returns char, with all letters lowercase. The return value has the same datatype as the argument char (CHAR or VARCHAR2). |
SELECT LOWER('Mr. Scott') AS "lower case" FROM DUAL; |
UPPER |
|
Returns char, with all letters uppercase. The return value has the same datatype as the argument char. |
SELECT UPPER('Mr. Scott') AS "UPPER CASE" FROM DUAL; |
LPAD |
|
Returns char1, left-padded to length n with the sequence of characters in char2; char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n. |
SELECT LPAD('Page',8,'*.') AS "LPAD example" FROM DUAL; |
RPAD |
|
Returns char1, right-padded to length n with char2, replicated as many times as necessary; char2 defaults to a single blank. If char1 is longer than n, this function returns the portion of char1 that fits in n. |
SELECT RPAD('Page',8,'*.') AS "RPAD example" FROM DUAL; |
LTRIM |
|
Removes characters from the left of char, with all the leftmost characters that appear in set removed; set defaults to a single blank. If char is a character literal, you must enclose it in single quotes. |
SELECT LTRIM('xyxYZ','xy') AS "LTRIM example" FROM DUAL; |
RTRIM |
|
Returns char, with all the rightmost characters that appear in set removed; set defaults to a single blank. If char is a character literal, you must enclose it in single quotes. |
SELECT RTRIM('xYZxy','xy') AS "RTRIM example" FROM DUAL; |
REPLACE |
|
Returns char with every occurrence of search string replaced with replace string. If replace string is omitted or null, all occurrences of search string are removed. If search is null, char is returned. This function provides a superset of the functionality provided by the TRANSLATE function. TRANSLATE provides single-character, one-to-one substitution. REPLACE lets you substitute one string for another as well as to remove character strings |
SELECT REPLACE('JACK and JUE','j', 'BL') AS "REPLACE example" FROM DUAL; |
SUBSTR |
|
Returns a portion of char, beginning at character m, n characters long. If mis 0, it is treated as 1. Ifmis positive,SQL counts from the beginning ofcharto find the first character. Ifmis negative, Oracle counts backwards from the end ofchar. Ifnis omitted,SQL returns all characters to the end ofchar. If n is less than 1, a null is returned. |
SELECT SUBSTR('ABCDE',2,2) AS "SUBSTR example" FROM DUAL; SELECT SUBSTR('ABCDE',-4,2) AS "SUBSTR example" FROM DUAL; |
TRANSLATE |
|
Returns char with all occurrences of each character in from replaced by its corresponding character in to. Characters in char that are not in from are not replaced. The argument from can contain more characters than to. In this case, the extra characters at the end of from have no corresponding characters in to. If these extra characters appear in char, they are removed from the return value. You cannot use an empty string for to to remove all characters in from from the return value. |
SELECT TRANSLATE('ABCDE', 'ABCD', '1234') AS TRANSLATE example" FROM DUAL; |
Character functions returning number values
Func-tion |
Syntax |
Purpose |
Example |
INSTR |
|
Searches char1 beginning with its nth character for the mth occurrence of char2 and returns the position of the character in char1 that is the first character of this occurrence. If n is negative, Oracle counts and searches backward from the end of char1. The value of m must be positive. The |
SELECT INSTR('CORPORATE FLOOR','OR','3','2') FROM DUAL; |
default values of both n and m are 1, meaning Oracle begins searching at the first character of char1 for the first occurrence of char2. The return value is relative to the beginning of char1, regardless of the value of n, and is expressed in characters. If the search is unsuccessful (if char2 does not appear m times after the nth character of char1) the return value is 0. | |||
LENGTH |
|
Returns the length of char in characters. If char has datatype CHAR, the length includes all trailing blanks. If char is null, this function returns null. |
SELECT INSTR('CORPORATE FLOOR','OR','3','2') FROM DUAL; |
ASCII |
|
Returns the decimal representation in the database character set of the first character of char. |
SELECT ASCII('Q') FROM DUAL; |