You are on page 1of 16

Section 4

CASE AND CHARACTER


MANIPULATION
LOWER SUBSTR
SELECT last_name SELECT SUBSTR('HelloWorld', 1, 5)
FROM employees FROM DUAL;
WHERE LOWER(last_name) = 'abel'; SELECT SUBSTR('HelloWorld', 6)
FROM DUAL;
SELECT SUBSTR(last_name, 1, 3)
UPPER
FROM employees;
SELECT last_name
FROM employees LENGTH
WHERE UPPER(last_name) = 'ABEL'; SELECT LENGTH('HelloWorld')
FROM DUAL;
SELECT LENGTH(last_name)
INITCAP FROM employees;
SELECT last_name
FROM employees INSTR
WHERE INITCAP(last_name) = 'Abel'; SELECT INSTR('HelloWorld', 'W')
FROM DUAL;
CONCAT SELECT last_name, INSTR(last_name, 'a')
SELECT CONCAT('Hello', 'World') FROM employees;
FROM DUAL;

SELECT CONCAT(first_name, last_name)


FROM employees;
LPAD REPLACE
SELECT LPAD('HelloWorld', 15, '-') REPLACE (string1, string_to_replace,
FROM DUAL; [replacement_string] )
SELECT LPAD(last_name, 10, '*')
FROM employees; SELECT REPLACE('JACK and JUE', 'J', 'BL')
FROM DUAL;
RPAD
SELECT RPAD('HelloWorld', 15, '-') SELECT REPLACE('JACK and JUE', 'J')
FROM DUAL; FROM DUAL;
SELECT RPAD(last_name, 10, '*')
FROM employees; SELECT REPLACE(last_name, 'a', '*')
FROM employees;
TRIM
SELECT TRIM(LEADING 'a' FROM 'abcba')
FROM DUAL;

SELECT TRIM(TRAILING 'a' FROM 'abcba')


FROM DUAL;

SELECT TRIM(BOTH 'a' FROM 'abcba')


FROM DUAL;
Substitution Variables
SELECT first_name, last_name, salary,
department_id
FROM employees
WHERE department_id= 10;

SELECT first_name, last_name, salary,


department_id
FROM employees
WHERE department_id=:enter_dept_id;
NUMBER FUNCTIONS
WHERE Clause

• The three number functions are:


– ROUND
– TRUNC
– MOD
ROUND(column|expression, decimal places)
• Note that if the number of decimal places is not specified or is
zero, the number will round to no decimal places.
ROUND(45.926) 46
ROUND(45.926, 0) 46
•If the number of decimal places is a positive number, the number
is rounded to that number of decimal places to the right of the
decimal point.
ROUND(45.926, 2) 45.93

•If the number of decimal places is a negative number, the


number is rounded to that number of decimal places to the left of
the decimal point.
ROUND(45.926, -1) 50
TRUNC (column|expression, decimal places)
•The TRUNC function can be used with both numbers and
dates. It is mainly used to terminate the column, expression,
or value to a specified number of decimal places.
•When TRUNC is used, if the number of decimal places is not
specified, then like ROUND, the specified number defaults to
zero.
TRUNC (45.926, 2) 45.92
MOD
The MOD function finds the remainder after one value is
divided by another value.

SELECT country_name, MOD(airports,2)


AS "Mod Demo"
FROM wf_countries;
DATE FUNCTION
DATE Data Type
• The DATE data type always stores year information as a
fourdigit number internally: two digits for the century and
two
digits for the year.
SELECT last_name, hire_date + 60 SYSDATE
FROM employees; SELECT SYSDATE
FROM dual;
SELECT last_name, (SYSDATE-hire_date)/7
FROM employees;

SELECT employee_id, (end_date-start_date)/365


AS "Tenure in last job"
FROM job_history;
Date Functions

MONTHS_BETWEEN
SELECT last_name, hire_date
FROM employees
WHERE MONTHS_BETWEEN
(SYSDATE, hire_date) > 240;
ADD_MONTHS ROUND
SELECT ADD_MONTHS (SYSDATE, 12) SELECT hire_date,
AS "Next Year" ROUND(hire_date, 'Month')
FROM dual; FROM employees
WHERE department_id = 50;
NEXT_DAY
SELECT NEXT_DAY (SYSDATE, 'Saturday') SELECT hire_date,
AS "Next Saturday" ROUND(hire_date, 'Year')
FROM dual; FROM employees
WHERE department_id = 50;
LAST_DAY
SELECT LAST_DAY (SYSDATE)
AS "End of the Month" TRUNC
FROM dual; SELECT hire_date,
TRUNC(hire_date, 'Month')
FROM employees
WHERE department_id = 50;

SELECT hire_date,
TRUNC(hire_date, 'Year')
FROM employees
WHERE department_id = 50;
MULTIPLE DATE FUNCTIONS

SELECT employee_id, hire_date,


ROUND(MONTHS_BETWEEN(SYSDATE, hire_date)) AS TENURE,
ADD_MONTHS (hire_date, 6) AS REVIEW,
NEXT_DAY(hire_date, 'FRIDAY'), LAST_DAY(hire_date)
FROM employees
WHERE MONTHS_BETWEEN (SYSDATE, hire_date) > 36;

You might also like