Professional Documents
Culture Documents
Lab 07 - Database Systems
Lab 07 - Database Systems
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”
INSTR
--------
4
g. REPLACE
Replaces a given set of characters in a string with another set of characters.
Example
REPLACE
---------
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
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
Ltrim
--------------
Interface
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
RTRIM
Trims blank spaces from a given string from Right.
Example
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.
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.
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
Absolute
----------
87
CEIL
Returns the lowest integer above the given number.
Example:
Ceil
---------
4
FLOOR
Example:
Floor
------------
3
MOD
Example
MOD
---------
2
POWER
Example
POWER
---------
8
EXP
Example
E RAISED TO 2
-------------
LN
Example
LN
------------
LOG
Example
LOG
---------
2
ROUND
Example
Round
------------
3.46
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
TRUNC
Example
SQRT
Example
SQRT
---------
4
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
DATE Function
Add_months:
add_months( date1, n )
date1 is the starting date (before the n months have been added).
Last_day:
The last_day function returns the last day of the month based on a date value.
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.
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.
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:
Example #2:
Example #3:
would return 0
Example #4:
would return 2
Conversion Function:
NATIONAL UNIVERSITY OF MODERN LANGUAGES
DEPARTMENT OF COMPUTER SCIENCES
To_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:
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
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