SQL 2 - More Functions and WHERE Clause
SQL 2 - More Functions and WHERE Clause
TABLES
Selected Character Functions
• INITCAP( )
• LOWER( )
• UPPER( )
• LENGTH( )
• SUBSTR( )
• INSTR( )
• LPAD( ) RPAD( )
• LTRIM( ) RTRIM( )
• REPLACE( )
More Character functions
SELECT SUBSTR(ename,1,3)
FROM EMP;
The name
of the The starting
column you position for The number of characters
are your you are extracting; if
extracting extraction absent, assumes
characters everything from
from character 1 onwards
SUBSTR( ) function
To display the last 4 characters of every employee name:
DISTINCT || (Concatination)
DATE FUNCTIONS
• SYSDATE
• MONTHS_BETWEEN( )
SELECT hiredate
• ADD_MONTHS ( ) FROM emp;
• TO_CHAR ( )
• TO_DATE ( )
Date Functions
• Default display for date in Oracle is DD-MON-YY
• It is stored as a number and includes the year,
month, day, minutes and seconds
• Valid dates are between 1-JAN-4712 B.C. and 31-
DEC-9999 A.D.
• Because dates are stored as numbers, you can do
‘maths’ with them (the result is in days) eg: from the
ORD table:
SELECT ordid, shipdate – orderdate
FROM ord;
ADD_MONTHS function
Adds given no. of months to a date field, eg
SELECT hiredate, ADD_MONTHS (hiredate, 24)
FROM emp;
Adds 24 months to
the hiredate field
The TO_CHAR Function
This is used for displaying dates in a particular format (rather
than the Oracle default of ‘DD-MON-YY’, by specifying a format
mask.
TO_CHAR (datefield, ‘format mask’)
Elements of a format
mask include:
TO_CHAR function
Egs:
SELECT TO_CHAR(orderdate, 'DAY, MONTH, YEAR')
FROM ord;
NUMBER FUNCTIONS
• ROUND( )
• TRUNC ( )
NVL and DECODE functions
NVL function
NULL values can cause problems in calculations, as any calculation involving a
NULL always equates to NULL. eg
SELECT empno, sal, comm, sal + comm
FROM emp;
You can see here that anywhere
where there is a NULL
commission gives a SAL+COMM
result of NULL
NVL function allows you to substitute a value for any NULLs found in a column, eg:
SELECT empno, sal, comm, sal + NVL(comm, 0)
FROM emp; This will look for
any NULLs in the ..and
comm column substitute
them with a 0
DECODE function
Allows you to ‘decode’ a field: Eg, supposing you wanted to display a list of
employees and the name of their departments, using just the emp table:
(assumes you know the names of the deparments)
SELECT ename
FROM emp
WHERE ename = ‘_A%’
Retrieving NULL values
NULL means the absence of data from a field – it’s not the same
as a space. When searching for NULL values the IS NULL
comparison operator is used.
Eg to search for employees who do not have a manager (mgr):
If you use = NULL the query will run but will not retrieve any
records:
SELECT ename, mgr
FROM emp
WHERE mgr = NULL;
Exercises – using the
‘CUSTOMER’ table:
1. List the names of all customers
in the ‘SW’ area.
2. List the customer IDs of all
customers with a credit limit of
between 1000 and 3000.
3. List full details of all the
customers who live in Bath,
Bristol or London
4. List full details of customers who
live in the ‘SW’ area or have staff
no. 7889 (repid) as their rep.
More Exercises
1. The name of employees who are salesmen.