Você está na página 1de 8

Week 4

1. CHARACTER OR TEXT FUNCTIONS:


Character or text functions are used to manipulate text strings. They accept strings or
characters as input and can return both character and number values as output.
Few of the character or text functions are as given below:
Function Name

Return Value

LOWER (string_value) All the letters in 'string_value' are converted to lowercase.


UPPER (string_value)

All the letters in 'string_value' are converted to uppercase.

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

TRIM (trim_text FROMAll


occurrences
of 'trim_text' from
the
left
and
right
string_value)
of 'string_value' ,'trim_text' and can be only one character long .
SUBSTR
m, n)

(string_value,Returns 'n' number of characters from'string_value' starting from the


'm'position.

LENGTH (string_value) Number of characters in 'string_value' is returned.


LPAD (string_value, n,Returns 'string_value' left-padded with'pad_value' . The length of the
pad_value)
whole string will be of 'n' characters.
RPAD (string_value, n,Returns 'string_value' right-padded with 'pad_value' . The length of the
pad_value)
whole string will be of 'n' characters.

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)

LTRIM ('Good Morning', 'Good)

Morning

RTRIM (string_value, trim_text)

RTRIM ('Good Morning', ' Morning') Good

TRIM (trim_text FROM string_value) TRIM ('o' FROM 'Good Morning')

Gd Mrning

SUBSTR (string_value, m, n)

SUBSTR ('Good Morning', 6, 7)

Morning

LENGTH (string_value)

LENGTH ('Good Morning')

12

LPAD (string_value, n, pad_value)

LPAD ('Good', 6, '*')

**Good

RPAD (string_value, n, pad_value)

RPAD ('Good', 6, '*')

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,

Returns the number of months between dates x1 and x2.

LAST_DAY (x)

It is used to determine the number of days remaining in a month


from the date 'x' specified.

SYSDATE

Returns the systems current date and time.

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

MONTHS_BETWEEN( ) MONTHS_BETWEEN ('16-Sep-81', '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])

Converts Numeric and Date values to a character string value. It


cannot be used for calculations since it is a string value.

TO_DATE
date_format])
NVL (x, y)

(x

[,Converts a valid Numeric and Character values to a Date value. Date


is formatted to the format specified by 'date_format'.
If 'x' is NULL, replace it with 'y'. 'x' and 'y'must be of the same
datatype.

DECODE (a, b, c, d, e, Checks the value of 'a', if a = b, then returns'c'. If a = d, then


default_value)
returns 'e'. Else, returnsdefault_value.

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;

TO_CHAR FUNCTION EXAMPLES - WITH DATES

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

Abbreviated name of month.

MONTH

Name of month, padded with blanks to length of 9 characters.

RM

Roman numeral month (I-XII; JAN = I).

Day of week (1-7).

DAY

Name of day.

DD

Day of month (1-31).

DDD

Day of year (1-366).

DY

Abbreviated name of day.

The following are date examples for the TO_CHAR function.


TO_CHAR(sysdate, 'yyyy/mm/dd');

would return '2003/07/09'

TO_CHAR(sysdate, 'Month DD, YYYY');

would return 'July 09, 2003'

TO_CHAR(sysdate, 'FMMonth DD, YYYY');

would return 'July 9, 2003'

TO_CHAR(sysdate, 'MON DDth, YYYY');

would return 'JUL 09th, 2003'

TO_CHAR(sysdate, 'FMMON DDth, YYYY');

would return 'JUL 9th, 2003'

TO_CHAR(sysdate, 'FMMon ddth, YYYY');

would return 'Jul 9th, 2003'

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');

would return 'July 9, 2003'

TO_CHAR(sysdate, 'FMMON DDth, YYYY');

would return 'JUL 9th, 2003'

TO_CHAR(sysdate, 'FMMon ddth, YYYY');

would return 'Jul 9th, 2003'

Lab practice queries


Before start solving the below queries just run the below script in the putty one by one

CREATE TABLE s_dept


(id

NUMBER(7)

CONSTRAINT s_dept_id_nn NOT NULL,


name

VARCHAR2(25)

CONSTRAINT s_dept_name_nn NOT NULL,


region_id

NUMBER(7),

CONSTRAINT s_dept_id_pk PRIMARY KEY (id),


CONSTRAINT s_dept_name_region_id_uk UNIQUE (name, region_id));

INSERT INTO s_dept VALUES (10, 'Finance', 1);


INSERT INTO s_dept VALUES ( 31, 'Sales', 1);
INSERT INTO s_dept VALUES (32, 'Sales', 2);
INSERT INTO s_dept VALUES ( 33, 'Sales', 3);
INSERT INTO s_dept VALUES (34, 'Sales', 4);
INSERT INTO s_dept VALUES (35, 'Sales', 5);
INSERT INTO s_dept VALUES (41, 'Operations', 1);
INSERT INTO s_dept VALUES (42, 'Operations', 2);
INSERT INTO s_dept VALUES ( 43, 'Operations', 3);
INSERT INTO s_dept VALUES ( 44, 'Operations', 4);
INSERT INTO s_dept VALUES ( 45, 'Operations', 5);
INSERT INTO s_dept VALUES (50, 'Administration', 1);
COMMIT;
CREATE TABLE s_emp
(id

NUMBER(7)

CONSTRAINT s_emp_id_nn NOT NULL,


last_name

VARCHAR2(25)

CONSTRAINT s_emp_last_name_nn NOT NULL,


first_name
userid
start_date

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),

CONSTRAINT s_emp_id_pk PRIMARY KEY (id),


CONSTRAINT s_emp_userid_uk UNIQUE (userid),
CONSTRAINT s_emp_commission_pct_ck
CHECK (commission_pct IN (10, 12.5, 15, 17.5, 20)));

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);

'08-MAR-90', NULL, 1, 'VP, Operations',41, 1450,

3, 'Nagayama', 'Midori', 'mnagayam', '17-JUN-91', NULL, 1, 'VP, Sales',31, 1400,

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);

'18-JAN-91', NULL, 2, 'Warehouse Manager',41,

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;

3. select to_char(sysdate,'YYYY') C1,to_char(sysdate,'YEAR') C2,to_char(sysdate,'MM')


C3,to_char(sysdate,'DY') C4,to_char(sysdate,'DAY') C5 from dual;

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 ;

Você também pode gostar