Escolar Documentos
Profissional Documentos
Cultura Documentos
Return Value
INITCAP (string_value) All the letters in 'string_value' are converted to mixed case.
LTRIM
(string_value,All occurrences
trim_text)
of 'string_value'.
of 'trim_text'
are
RTRIM
(string_value,All occurrences
trim_text)
of'string_value' .
of 'trim_text'
are
removed
removed
from
from
the
the
left
right
The following examples explain the usage of above mentioned character or text functions
Function Name
Examples
Return Value
LOWER(string_value)
LOWER('Good Morning')
good morning
UPPER(string_value)
UPPER('Good Morning')
GOOD MORNING
INITCAP(string_value)
INITCAP('GOOD MORNING')
Good Morning
LTRIM(string_value, trim_text)
Morning
Gd Mrning
SUBSTR (string_value, m, n)
Morning
LENGTH (string_value)
12
**Good
Good**
2. DATE FUNCTIONS:
These are functions that take values that are of data type DATE as input and return values of
data types DATE, except for the MONTHS_BETWEEN function, which returns a number
as output.
Few date functions are as given below.
Function Name
Return Value
ADD_MONTHS (date, n)
Returns a date value after adding 'n' months to the date 'x'.
MONTHS_BETWEEN
x2)
(x1,
LAST_DAY (x)
SYSDATE
The below table provides the examples for the above functions
Function Name
Examples
Return Value
ADD_MONTHS ( )
ADD_MONTHS ('16-Sep-81', 3)
16-Dec-81
LAST_DAY( )
30-Jun-08
LAST_DAY ('01-Jun-08')
3. CONVERSION FUNCTIONS:
Few of the conversion functions available in oracle are:
Function Name
Return Value
TO_CHAR (x [,y])
TO_DATE
date_format])
NVL (x, y)
(x
COALESCE
COALESCE function, a more generic form of NVL, returns the first non-null expression in the
argument list. It takes minimum two mandatory parameters but maximum arguments has no
limit.
Syntax:
COALESCE (expr1, expr2, ... expr_n )
Consider the below SELECT query. It selects the first not null value fed into address fields for an
employee.
SELECT COALESCE (address1, address2, address3) Address
FROM employees;
Interestingly, the working of COALESCE function is similar to IF..ELSIF..ENDIF construct. The
query above can be re-written as IF address1 is not null THEN
result := address1;
ELSIF address2 is not null THEN
result := address2;
ELSIF address3 is not null THEN
result := address3;
ELSE
result := null;
END IF;
The following is a list of valid parameters when the TO_CHAR function is used to convert a
date to a string. These parameters can be used in many combinations.
MON
MONTH
RM
DAY
Name of day.
DD
DDD
DY
You will notice that in some TO_CHAR function examples, the format_mask parameter begins
with "FM". This means that zeros and blanks are suppressed. This can be seen in the examples
below.
TO_CHAR(sysdate, 'FMMonth DD, YYYY');
NUMBER(7)
VARCHAR2(25)
NUMBER(7),
NUMBER(7)
VARCHAR2(25)
VARCHAR2(25),
VARCHAR2(8),
DATE,
comments
VARCHAR2(255),
manager_id
NUMBER(7),
title
VARCHAR2(25),
dept_id
salary
commission_pct
NUMBER(7),
NUMBER(11, 2),
NUMBER(4, 2),
INSERT INTO s_emp VALUES (1, 'Velasquez', 'Carmen', 'cvelasqu', to_date('03-MAR-90 8:30', 'dd-mon-yy hh24:mi'),
NULL, NULL, 'President',50, 2500, NULL);
INSERT INTO s_emp VALUES ( 2, 'Ngao', 'LaDoris', 'lngao',
NULL);
INSERT INTO s_emp VALUES (
NULL);
INSERT INTO s_emp VALUES (4, 'Quick-To-See', 'Mark', 'mquickto', '07-APR-90', NULL, 1, 'VP, Finance', 10, 1450,
NULL);
INSERT INTO s_emp VALUES (5, 'Ropeburn', 'Audry', 'aropebur', '04-MAR-90', NULL, 1, 'VP, Administration', 50,
1550, NULL);
INSERT INTO s_emp VALUES (6, 'Urguhart', 'Molly', 'murguhar',
1200, NULL);
INSERT INTO s_emp VALUES (7, 'Menchu', 'Roberta', 'rmenchu','14-MAY-90', NULL, 2, 'Warehouse Manager', 42,
1250, NULL);
INSERT INTO s_emp VALUES (8, 'Biri', 'Ben', 'bbiri', '07-APR-90', NULL, 2, 'Warehouse Manager',43, 1100, NULL);
INSERT INTO s_emp VALUES (9, 'Catchpole', 'Antoinette', 'acatchpo','09-FEB-92', NULL, 2, 'Warehouse Manager', 44,
1300, NULL);
INSERT INTO s_emp VALUES (10, 'Havel', 'Marta', 'mhavel', '27-FEB-91', NULL, 2, 'Warehouse Manager', 45, 1307,
NULL);
INSERT INTO s_emp VALUES (11, 'Magee', 'Colin', 'cmagee','14-MAY-90', NULL, 3, 'Sales Representative', 31, 1400,
10);
INSERT INTO s_emp VALUES (12, 'Giljum', 'Henry', 'hgiljum', '18-JAN-92', NULL, 3, 'Sales Representative', 32, 1490,
12.5);
INSERT INTO s_emp VALUES (13, 'Sedeghi', 'Yasmin', 'ysedeghi','18-FEB-91', NULL, 3, 'Sales Representative',33,
1515, 10);
INSERT INTO s_emp VALUES (14, 'Nguyen', 'Mai', 'mnguyen','22-JAN-92', NULL, 3, 'Sales Representative', 34, 1525,
15);
INSERT INTO s_emp VALUES (15, 'Dumas', 'Andre', 'adumas', '09-OCT-91', NULL, 3, 'Sales Representative', 35, 1450,
17.5);
INSERT INTO s_emp VALUES (16, 'Maduro', 'Elena', 'emaduro','07-FEB-92', NULL, 6, 'Stock Clerk',41, 1400, NULL);
INSERT INTO s_emp VALUES (17, 'Smith', 'George', 'gsmith', '08-MAR-90', NULL, 6, 'Stock Clerk', 41, 940, NULL);
INSERT INTO s_emp VALUES (18, 'Nozaki', 'Akira', 'anozaki', '09-FEB-91', NULL, 7, 'Stock Clerk',42, 1200, NULL);
INSERT INTO s_emp VALUES (19, 'Patel', 'Vikram', 'vpatel', '06-AUG-91', NULL, 7, 'Stock Clerk', 42, 795, NULL);
INSERT INTO s_emp VALUES (20, 'Newman', 'Chad', 'cnewman', '21-JUL-91', NULL, 8, 'Stock Clerk', 43, 750, NULL);
INSERT INTO s_emp VALUES (21, 'Markarian', 'Alexander', 'amarkari', '26-MAY-91', NULL, 8, 'Stock Clerk', 43, 850,
NULL);
INSERT INTO s_emp VALUES ( 22, 'Chang', 'Eddie', 'echang','30-NOV-90', NULL, 9, 'Stock Clerk',44, 800, NULL);
INSERT INTO s_emp VALUES (23, 'Patel', 'Radha', 'rpatel','17-OCT-90', NULL, 9, 'Stock Clerk',34, 795, NULL);
INSERT INTO s_emp VALUES (24, 'Dancs', 'Bela', 'bdancs','17-MAR-91', NULL, 10, 'Stock Clerk',45, 860, NULL);
INSERT INTO s_emp VALUES (25, 'Schwartz', 'Sylvie', 'sschwart', '09-MAY-91', NULL, 10, 'Stock Clerk',45, 1100,
NULL);
COMMIT;
Practice Questions
Week 4:
1. Display first name ,last name and salary from s_emp where salary should display in the
format $99,999;
2. Write query to display first name and last name as full name, salary *12+ commission_pct
*salary as Total Salary from s_emp.If commission_pct *salary is null then consider it as
zero use nvl function;
4. write query to display employee name ,job, salary and revised salary from s_emp
If job is ANALYST then revised salary = salary*1.03
If job is MANAGER then revised salary = salary*1.1
If job is PRESIDENT then revised salary = salary*1.01
Else display revised salary = salary
(Use decode function)
5. display first name , last name as full name ,title , salary and revised salary from
scott.s_emp table
If title is 'Sales Representative' then make revised salary = salary*1.03
If 'VP, Administration' then make revised salary = salary*1.02
If 'Warehouse Manager' then make revised salary = salary*1.1
If 'Stock Clerk' then make revised salary = salary*1.01
Else display revised salary = salary*1.05
(Use case function)
6. Display word gOwoLFpack in 3 ways by using Initcap , upper and lower functions;
7. select substr('ABCDEFG',0)
S1,substr('ABCDEFG',1) S2,substr('ABCDEFG',4)
S3,substr('ABCDEFG',4,3) S4,substr('ABCDEFG',-5)
S5,substr('ABCDEFG',-5,4)
S6,substr('ABCDEFG',-5,10) S7,substr('ABCDEFG',-5,-1) S8 from dual;
8. Display salary and if salary is null display commission and if both are null display 0 using
coalesce function from s_emp ;