Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

NATIONAL UNIVERSITY OF MODERN LANGUAGES

DEPARTMENT OF COMPUTER SCIENCES

DATABASE MANAGEMENT SYSTEMS


LAB 07
LAB TITLE

Single Row Functions

Lab Objectives Single Row Functions

Describe various types of functions available in SQL


Lab Task/Experiment Use character and number in SELECT statements

Equipment Oracle SQL

Experiment Details & Procedure:


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.

1. Character Functions
Character functions operate on values of dataype  CHAR or VARCHAR.
a. LOWER
Returns a given string in lower case.
select LOWER(‘SAMI’) from dual;
LOWER
-------------
sami
b. UPPER
Returns a given string in UPPER case.
select UPPER(‘Sami’) from dual;
UPPER
------------------
SAMI             
c. INITCAP
Returns a given string with Initial letter in capital.
select INITCAP(‘mohammed sami’) from dual;
INITCAP
------------------
Mohammed Sami
 

d. LENGTH
Returns the length of a given string.
select length(‘mohammed sami’) from dual;
LENGTH
------------
        13
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

e. SUBSTR
Returns a substring from a given string. Starting from position p to n characters.
For example the following query returns “sam” from the string “mohammed sami”.
select substr('mohammed sami',10,3) from dual;
Substr
--------
sam

f. 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.

Example

The following query tests whether the character “a” occurs in string “mohammed sami”

select instr('mohammed sami','a') from dual;

INSTR
--------
4

g. REPLACE
Replaces a given set of characters in a string with another set of characters.

Example

The following query replaces “mohd” with “mohammed” .

select replace('ali mohd khan','mohd','mohammed') from dual;

REPLACE
---------
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

ali mohammed khan

 
h. TRANSLATE
This function is used to encrypt characters. For example you can use this function to replace characters in a given
string with your coded characters.

Example

The following query replaces characters A with B, B with C, C with D, D with E,...Z with A, and a with b,b with c,c
with d, d with e ....z with a.

select
translate('interface','ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz','BCDEFGHIJKLMNO
PQRSTUVWXYZAbcdefghijklmnopqrstuvwxyza') “Encrypt” from dual;

Encrypt
-----------
joufsgbdf
 

LTRIM
Trims blank spaces from a given string from left.

Example

The following query returns string “       Interface        “ left trimmed.

select ltrim('       Interface       ') from dual;

Ltrim
--------------
Interface 
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

RTRIM
Trims blank spaces from a given string from Right.

Example

The following query returns string “       Interface        “ right trimmed.

select rtrim('       Interface       ') from dual;

Rtrim
------------
   Interface 

TRIM
Trims a given character from left or right or both from a given string.

Example

The following query removes zero from left and right of a given string.

Select trim(0 from '00003443500') from dual;

Trim
----------
34435
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

CONCAT
Combines a given string with another string.

Example:

Number Functions

Number functions accept numeric input and return numeric values. Most of these functions return values
that are accurate to 38 decimal digits.

The number functions available in Oracle are:

ABS  ACOS ASIN ATAN ATAN2 BITAND CEIL COS COSH EXP FLOOR LN LOG
MOD POWER ROUND (number) SIGN SIN SINH SQRT TAN TANH TRUNC (number)
 
ABS

ABS returns the absolute value of n.

The following example returns the absolute value of -87:

SELECT ABS(-87) "Absolute" FROM DUAL;

 Absolute
----------
        87
CEIL
Returns the lowest integer above the given number.

Example:

The following function return the lowest integer above 3.456;

select ceil(3.456) “Ceil” from dual;


NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

Ceil
---------
        4
FLOOR         

Returns the highest integer below the given number.

Example:

The following function return the highest integer below 3.456;

select floor(3.456) “Floor” from dual;

Floor
------------
        3
MOD

Returns the remainder after dividing m with n.

Example

The following example returns the remainder after dividing 30 by 4.

Select mod(30,4) “MOD” from dual;

MOD
---------
        2
POWER

Returns the power of m, raised to n.

Example

The following example returns the 2 raised to the power of 3.

select  power(2,3) “Power” from dual;

POWER
---------
        8
EXP

Returns the e raised to the power of n.

Example

The following example returns the e raised to power of 2.


NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

select exp(2) “e raised to 2” from dual;

E RAISED TO 2
-------------

       

LN

Returns natural logarithm of n.

Example

The following example returns the natural logarithm of 2.

select ln(2) from dual;

LN
------------

LOG

Returns the logarithm, base m, of n 

Example

The following example returns the log of 100.

select log(10,100) from dual;

LOG
---------
        2
ROUND

Returns a decimal number rounded of to a given decimal positions.

Example

The following example returns the no. 3.4573 rounded to 2 decimals.

select round(3.4573,2) “Round” from dual;

Round
------------
        3.46
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

TRUNC

Returns a decimal number Truncated to a given decimal positions.

Example

The following example returns the no. 3.4573 truncated to 2 decimals.

SQRT

Returns  the square root of a given number.

Example

The following example returns the square root of  16.

select  sqrt(16) from dual;

SQRT
---------
        4
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

DATE Function

Add_months:

The add_months function returns a date plus n months.

The syntax for the add_months function is:

add_months( date1, n )

date1 is the starting date (before the n months have been added).

n is the number of months to add to date1.

Last_day:

The last_day function returns the last day of the month based on a date value.

The syntax for the last_day function is:

last_day( date )

date is the date value to use to calculate the last day of the month.

Next_day:

The next_day function returns the first weekday that is greater than a date.

The syntax for the next_day function is:

next_day( date, weekday )

date is used to find the next weekday.


NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

weekday is a day of the week (ie: SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY,
FRIDAY, SATURDAY)

Months_between:

The months_between function returns the number of months between date1 and date2.

The syntax for the months_between function is:

months_between( date1, date2 )

date1 and date2 are the dates used to calculate the number of months.

If a fractional month is calculated, the months_between function calculates the fraction based on a 31-day
month.

Example #1:

months_between (to_date ('2003/01/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return -2.41935483870968

Example #2:

months_between (to_date ('2003/07/01', 'yyyy/mm/dd'), to_date ('2003/03/14', 'yyyy/mm/dd') )

would return 3.58064516129032

Example #3:

months_between (to_date ('2003/07/02', 'yyyy/mm/dd'), to_date ('2003/07/02', 'yyyy/mm/dd') )

would return 0

Example #4:

months_between (to_date ('2003/08/02', 'yyyy/mm/dd'), to_date ('2003/06/02', 'yyyy/mm/dd') )

would return 2

Conversion Function:
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

To_date:

The to_date function converts a string to a date.

The syntax for the to_date function is:

to_date( string1, [ format_mask ], [ nls_language ] )

string1 is the string that will be converted to a date.

format_mask is optional. This is the format that will be used to convert string1 to a date.

nls_language is optional. This is the nls language used to convert string1 to a date.

To_number:

The to_number function converts a string to a number.

The syntax for the to_number function is:

to_number( string1, [ format_mask ], [ nls_language ] )

string1 is the string that will be converted to a number.

format_mask is optional. This is the format that will be used to convert string1 to a number.

nls_language is optional. This is the nls language used to convert string1 to a number.

To_char

The to_char function converts a number or date to a string.

The syntax for the to_char function is:

to_char( value, [ format_mask ], [ nls_language ] )

value can either be a number or date that will be converted to a string.

format_mask is optional. This is the format that will be used to convert value to a string.
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

nls_language is optional. This is the nls language used to convert value to a string.

Database Systems

Lab Tasks

Lab Task 01:


1. Create table name test
a. create table test
( id number,
t_date date);
2. Now Insert values in table TEST.
a. insert into test
values(1, to_date('11-10-88','MM-DD-YY') );
b. insert into test
values(2, to_date('11-20-88','MM-DD-RR') );
c. insert into test
values(3, to_date('11-20-44','MM-DD-RR') );
d. insert into test
values(4, to_date('11-20-44','MM-DD-yy') );
3. Now display all rows from TEST
a. Select * from TEST;
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES

1. Create table name Employee having attributes


First_name
Last_name
address
Salary

Use following function and show results


SELECT CONCAT (first_name, last_name)
FROM employees

SELECT SUBSTR (first_name,1,5), INSTR (first_name,'a')


FROM employees

SELECT UPPER (first_name), INITCAP (last_name), LOWER (address)


FROM employees

You might also like