Site icon IT Tutorial

Oracle SQL Tutorials – Chapter 3 (Part 3 of 3)

CONVERSION FUNCTIONS

TO_CHAR (DATE)

SELECT hire_date,

TO_CHAR (hire_date, 'dd.mm.yyyy') "dd.mm.yyyy",

TO_CHAR (hire_date, 'DD Month YYYY') "DD Month YYYY",

TO_CHAR (hire_date, 'Ddspth Month YYYY HH:MI:SS')

"Ddspth Month YYYY HH:MI:SS "

FROM hr.employees;

 

TO_NUMBER and TO_DATE

It converts characters to number format.

 

It converts characters to date format.

 

SELECT

TO_NUMBER ('2000'),

SYSDATE,

TO_DATE ('18/07/2014', 'dd.mm.yyyy'),

TO_DATE ('20140315', 'yyyymmdd'),

TO_DATE ('070910', 'MMDDYY')

FROM DUAL;

 

NESTED FUNCTIONS

 

SELECT

first_name,

last_name,

UPPER (CONCAT (SUBSTR (LAST_NAME, 1, 6), '_US'))

FROM hr.employees;

 

NVL FUNCTION

SELECT

last_name,

salary,

NVL (commission_pct, 0),(salary * 12) +

(salary * 12 * NVL (commission_pct, 0)) AN_SAL

FROM hr.employees;

 

DECODE FUNCTION

 

15% to ‘ST_CLERK’ emplyoyees and

20% to ‘SA_REP’ employees.

 

SELECT last_name, job_id, salary,

DECODE (job_id,

'IT_PROG', 1.10 * salary,

'ST_CLERK', 1.15 * salary,

'SA_REP', 1.20 * salary,

salary)"Raised Salary"

FROM hr.employees

ORDER BY 1

 

CASE STATEMENT

 

SELECT first_name, last_name, job_id, salary,

CASE job_id

WHEN 'IT_PROG' THEN 1.10 * salary

WHEN 'ST_CLERK' THEN 1.15 * salary

WHEN 'SA_REP' THEN 1.20 * salary

ELSE salary

END "Raised Salary"

FROM hr.employees;

 

Exit mobile version