Escolar Documentos
Profissional Documentos
Cultura Documentos
1.
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
I N I T C A P
All the letters in 'string_value' are converted to mixed case.
(string_value)
LTRIM (string_value, All occurrences of 'trim_text'
trim_text)
of 'string_value'.
RTRIM (string_value, All occurrences of 'trim_text' are removed from the right
trim_text)
of'string_value' .
T R I M ( t r i m _ t e x t A l l o c c u r r e n c e s o f 'trim_text' f r o m t h e l e f t a
n d r i g h t of 'string_value' ,'trim_text' and can be only one
FROM string_value)
character long .
S U B S T R Returns 'n' number of characters from'string_value' starting from the
(string_value, m, n)
'm' position.
L E N G
(string_value)
For Example, we can use the above UPPER() text function with the column value as
follows.
SELECT UPPER (product_name) FROM product;
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')
G O O
MORNING
INITCAP(string_value)
INITCAP('GOOD MORNING')
Good Morning
LTRIM(string_value, trim_text)
RT R I M ( ' G o o d M o r n i n
Good
g ' , ' Morning')
d Morning
SUBSTR (string_value, m, n)
Morning
LENGTH (string_value)
12
**Good
Good**
2.
NUMBER FUNCTION:
Round
Round(43.659,2) 43.66
Trunc
Trunc(43.659,2)
43.65
Mod
Mod(1600,300)
100
3.
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.
Return Value
ADD_MONTHS (date, n)
MONTHS_BETWEEN (x1,
Returns the number of months between dates x1 and x2.
x2)
LAST_DAY (x)
SYSDATE
The below table provides the examples for the above functions
Function Name
Examples
R e t u r n
Value
ADD_MONTHS ( )
ADD_MONTHS ('16-Sep-81', 3)
16-Dec-81
LAST_DAY ('01-Jun-08')
4.
CONVERSION
FUNCTIONS:
Type Conversions
TO_DATE Convert a String to a Date
TO_NUMBER Convert a String to a Number
TO_CHAR Convert a Date or Number to a String
30-Jun-08
Return Value
TO_CHAR (x [,y])
If 'x' is NULL, replace it with 'y'. 'x' and 'y'must be of the same
datatype.
The below table provides the examples for the above functions
Function Name
Examples
Return Value
TO_DATE ()
$3000
Monday, Jun
e
2008
01-Jun-08
NVL ()
NVL (null, 1)
TO_CHAR ()
NVL2
As an enhancement over NVL, Oracle introduced a function to substitute value not only for
NULL columns values but also for NOT NULL columns. NVL2 function can be used to
substitute an alternate value for NULL as well as non NULL value.
Syntax:
NVL2( string1, value_if_NOT_null, value_if_null )
The SELECT statement below would display 'Bench' if the JOB_CODE for an employee is
NULL. For a definite not null value of JOB CODE, it would show constant value 'Job Assigned'.
SQL> SELECT NVL2(JOB_CODE,'Job Assigned','Bench')
FROM employees;
NULLIF
The NULLIF function compares two arguments expr1 and expr2. If expr1 and expr2 are equal, it
returns NULL; else, it returns expr1. Unlike the other null handling function, first argument can't
be NULL.
Syntax:
NULLIF (expr1, expr2)
Note that first argument can be an expression that evaluates to NULL, but it can't be the literal
NULL. Both the parameters are mandatory for the function to execute.
The below query returns NULL since both the input values, 12 are equal.
SELECT NULLIF (12,12)
FROM DUAL;
Similarly, below query return 'SUN' since both the strings are not equal.
SELECT NULLIF ('SUN','MOON')
FROM DUAL;
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;
Output:
777*888
--------689976
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
WW
Week of year (1-53) where week 1 starts on the first day of the year and
continues to the seventh day of the year.
Week of month (1-5) where week 1 starts on the first day of the month and
ends on the seventh.
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.
The zeros have been suppressed so that the day component shows as "9" as opposed to "09".
FREQUENTLY ASKED QUESTIONS (TO_CHAR
FUNCTION) Question: Why doesn't this sort the days of the week
in order?
select ename, hiredate, TO_CHAR((hiredate),'fmDay') "Day"
from emp
order by "Day";
Answer: In the above SQL, the fmDay format mask used in the TO_CHAR function will
return the name of the Day and not the numeric value of the day.
To sort the days of the week in order, you need to return the numeric value of the day by using
the fmD format mask as follows:
select ename, hiredate, TO_CHAR((hiredate),'fmD') "Day"
from emp
order by "Day";
1. AVG
2. COUNT
3. MAX
4. MIN
5. STDDEV
6. SUM
7. VARIANCE
You cant use group functions in the Select dept_id, count(last_name) from employee;
Column missing in the group by clause.
Note: Before start solving the below queries just run the below script in the putty one by one
NUMBER(7)
VARCHAR2(25)
NUMBER(7),
VARCHAR2(25),
VARCHAR2(8),
DATE,
comments
VARCHAR2(25),
manager_id
NUMBER(7),
title
dept_id
salary
commission_pct
VARCHAR2(25),
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',
'VP, Operations',41, 1450, NULL);
'08-MAR-90', NULL, 1,
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',
'Warehouse Manager',41, 1200, NULL);
'18-JAN-91', NULL, 2,
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
6. s e l e c t t o _ c h a r ( s y s d a t e , ' Y Y ' ) C 2 , t o _ c h a r ( s y
sdate,'RR')C3,to_char(sysdate,'MONTH') C
4,to_char(sysdate,'MON')C5,to_char(sysdat
e , ' R M ' ) C 6 , t o _ c h a r ( s y s d a t e , ' W W ' )
C7,to_char(sysdate,'W') C8 from dual; Explain the result.
Solution: W week of the month
WW- week of the year
RM Display the month in roman
number MON month (only 3
characters) MONTH display full
name of month
RR RR converts two-digit years into four-digit years by rounding
YY- It allows you to retrieve just two digits of a year, for example, the 99 in
1999
7. Display word gOwoLFpack in 3 ways by using Initcap , upper and
lower functions
8. 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; Explain the result.
substr('ABCDEFG',0)
display full string
10.write query to display employee name ,job, salary and revised salary
from s_emp
If job is Ware house manager then revised salary = salary*1.03
If job is Sales representative then revised salary = salary*1.1
If job is stock clerk then revised salary = salary*1.01
Else display revised salary = salary(Use decode function).[Extra Credit].