0% found this document useful (0 votes)
6 views33 pages

Functions-1 DBMS

The document provides an overview of SQL single-row functions, detailing their types, such as numeric, character, datetime, and conversion functions. It includes examples for each function, demonstrating their usage and output, such as ABS, CEIL, FLOOR, and string manipulation functions like LOWER and UPPER. Additionally, it covers date-related functions like ADD_MONTHS and conversion functions like TO_CHAR and NVL.

Uploaded by

sourabh.planetc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views33 pages

Functions-1 DBMS

The document provides an overview of SQL single-row functions, detailing their types, such as numeric, character, datetime, and conversion functions. It includes examples for each function, demonstrating their usage and output, such as ABS, CEIL, FLOOR, and string manipulation functions like LOWER and UPPER. Additionally, it covers date-related functions like ADD_MONTHS and conversion functions like TO_CHAR and NVL.

Uploaded by

sourabh.planetc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

SQL FUNCTIONS

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
and CONNECT BY clauses, and HAVING clauses.

 Type of Single-Row Functions:

Numeric Functions
Character Fucntions
Datetime Fucntions
Conversion Functions
ABSOLUTE
ABS returns the absolute value of n.

Ex:
select abs(-87) "Absolute" from dual;

Absolute
----------------
87
CEIL
CEIL returns the highest integer of the given number.

Ex:
select ceil(3.456) "Ceil" from dual;

Ceil
----------------
4
FLOOR
FLOOR returns the lowest integer of the given number.

Ex:
select floor(3.456) "Floor" from dual;

Floor
----------------
3
MOD
MOD returns the remainder after dividing m with n.

Ex:
select mod(30,4) "Mod" from dual;

Mod
----------------
2
POWER
POWER returns the power of m, raised to n.

Ex:
select power(2,3) "Power" from dual;

Power
----------------
8
ROUND
ROUND returns a decimal number rounded to a given
decimal positions.

Ex:
select round(3.4573,2) "Round" from dual;

Round
----------------
3.46
TRUNC
TRUNC returns a decimal number truncated to a given
decimal positions.

Ex:
select trunc(3.4573,2) "Trunc" from dual;

Trunc
----------------
3.45
SQRT
SQRT returns the square root of a given number.

Ex:
select sqrt(16) "Sqrt" from dual;

Sqrt
----------------
4
LOWER
LOWER returns a given string in lower case.

Ex:
select lower('HELLO') "Lower" from dual;

Lower
----------------
hello
UPPER
UPPER returns a given string in upper case.

Ex:
select upper('hello') "Upper" from dual;

Upper
----------------
HELLO
INITCAP
INITCAP returns a given string with Initial letter in
capital.

Ex:
select initcap('database management system') "Init Cap"
from dual;

Init Cap
----------------
Database Management System
LENGTH
LENGTH returns the length of a given string.

Ex:
select length('database management system') "Length"
from dual;

Length
----------------
26
SUBSTR
SUBSTR returns a substring from a given string,
starting from position p to n characters.

Ex:
select substr('database management system',10,3)
"Substr" from dual;

Substr
----------------
man
INSTR
INSTR tests whether a given character occurs in the
given string or not. If the character occurs in the string
then returns the first position of its occurrence
otherwise returns 0.

Ex:
select instr('database management system','a') "Instr"
from dual;

Instr
----------------
2
REPLACE
REPLACE a given set of characters in a string with
another set of characters.

Ex:
select replace('Information System','System','Technology')
"Replace" from dual;

Replace
----------------
Information Technology
TRANSLATE

The SQL TRANSLATE function replaces a sequence of


characters in a string with another sequence of
characters.

Ex:
SELECT TRANSLATE( '+91 25-2469782464',
'0123456789-+','6789012345+-' ) "encode_number"
FROM dual;

Encode_number
-----------------------
-57 81+8025348020
LTRIM
LTRIM trims blank spaces from a given string from left.

Ex:
select ltrim(' Hello ') "Ltrim" from dual;

Ltrim
----------------
Hello
RTRIM
RTRIM trims blank spaces from a given string from
Right.

Ex:
select rtrim(' Hello ') "Rtrim" from dual;

Ltrim
----------------
Hello
TRIM
TRIM trims a given character from left or right or both
from a given string.

Ex:
select trim(' Hello ') “Trim" from dual;

Trim
----------------
Hello
LPAD

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.

Ex:
SELECT LPAD('Page 1',15,'*.') "LPAD"
FROM DUAL;

LPAD
---------------------
*.*.*.*.*Page 1
RPAD

RPAD returns expr1, right-padded to length n


characters with the sequence of characters in expr2.
This function is useful for formatting the output of a
query.

Ex:
SELECT RPAD('Page 1',15,'*.') “RPAD"
FROM DUAL;

RPAD
---------------------
Page 1 *.*.*.*.*
CONCAT

CONCAT returns char1 concatenated with char2.

Ex:
SELECT CONCAT('Single',' Lane') “Merge" from dual;

Merge
------------------
Single Lane
GREATEST

GREATEST returns greatest value from the list.


Ex:
select greatest(10,20,50,20,30) "Greatest" from dual;
Greatest
----------------
50
Ex:
select greatest('SAMI','SCOTT','RAVI','SMITH','TANYA')
"Greatest" from dual;
Greatest
----------------
TANYA
LEAST
LEAST returns least value from the list.
Ex:
select least(10,20,50,20,30) "Least" from dual;
Least
----------------
10
Ex:
select least('SAMI','SCOTT','RAVI','SMITH','TANYA') "Least"
from dual;
Least
----------------
RAVI
ADD_MONTHS

ADD_MONTHS returns the date “date” plus “integer”


months.

Ex:
SELECT Add_Months(sysdate,1) “Next Month" from
dual;

Next Month
------------------
16-Oct-16
EXTRACT

EXTRACT extracts and returns the value of a specified


datetime field from a datetime or interval expression.

Ex:
SELECT EXTRACT(month FROM sysdate) “Month”
FROM DUAL;
Result: 9

SELECT EXTRACT(YEAR FROM DATE '1998-03-07')


FROM DUAL;
Result: 1998
TO_CHAR(DATETIME)

TO_CHAR (datetime) converts a datetime.

Ex:
SELECT TO_CHAR(sysdate, 'DD-MON-YYYY’) AS
“cur_date” FROM dual
Result: 16-sep-2016

SELECT TO_CHAR(sysdate, 'DD-MON-YYYY


HH24:MI:SS') AS “cur_date” FROM dual
Result: 16-SEP-2016 11:30:25
TO_DATE(DATETIME)

TO_DATE converts char of CHAR, VARCHAR2 data


type to a value of DATE data type.

Ex:
SELECT TO_DATE('January 15, 1989, 11:00 A.M.',
'Month dd, YYYY, HH:MI A.M.') FROM DUAL;
Result: 15-JAN-1989

SELECT TO_CHAR(sysdate, 'DD-MON-YYYY


HH24:MI:SS') AS “cur_date” FROM dual
Result: 16-SEP-2016 11:30:25
COALESCE

COALESCE returns the first non-null expr in the


expression list. You must specify at least two
expressions. If all occurrences of expr evaluate to null,
then the function returns null.

Ex:
SELECT COALESCE(NULL, NULL, 23+5) from dual;
Result: 23

Ex:
SELECT COALESCE(lname, fname, NULL) from staff
NVL

NVL lets you replace null (returned as a blank) with a


string in the results of a query.

Ex:
SELECT NVL(NULL, 'NODATA') from dual;
Result: NODATA

Ex:
SELECT NVL(lname, 'NA') from staff;
NVL2

NVL2 lets you determine the value returned by a query


based on whether a specified expression is null or not
null.

Ex:
SELECT NVL2('HELLO', 'DATA', 'NODATA') from dual;
Result: DATA

Ex:
SELECT NVL2(lname, 'DATA', 'NA') from staff;

You might also like