Oracle Day 5 - Single Row Functions
Oracle Day 5 - Single Row Functions
Note: Please watch my below-mentioned YouTube sessions for a better understanding of the
queries provided below.
Oracle SQL Day 2 – SQL Types DDL, DML, DRL, DCL, TCL - https://youtu.be/XpgjXvnfZec
select first_name,upper(first_name),lower(first_name),initcap(first_name),
substr(string,from_position,no_of_char); -- 3 argument
substr(string,from_position); -- 2 argument
instr('When system dialog prompts, click Open Zoom Meetings.',',')-1) from dual;
job:
===
Manager BOSS
manager BOSS
MANAGER BOSS
select phone_number,'+1-'||substr(replace(phone_number,'.',''),1,5)||'-
'||substr(replace(phone_number,'.',''),6,5)||'-'||
515.123.4567
select phone_number,
case
else
'+1-'||substr(replace(phone_number,'.',''),1,5)||'-'||substr(replace(phone_number,'.',''),6,5)||'-'||
ABCD XYZ
A--> X
B--> Y
C--> Z
D--> NUll
A W
B X
C Y
D Z
E null
F null
WLYOM TO YHNNWI
---------------------------------------------------------------------------------------------
-------------------------------------------------------------------
NVL - 2 arg
NVL2 - 3 arg
Nullif - 2 arg
NVL(arg1,arg2)
NVL2(arg1,arg2,arg3)
from employee;
-----------------------------------------------------------
nullif(arg1,arg2)
-----------------------------------------------------------
select commission_pct,manager_id,department_id,
--------------------------------------------------------------------------------
----------------------------------------------------
--------------------------------------------------------------
----------------------------------------------------------------------------
-- MONTHS_BETWEEN(date1,date2)
sysdate=last_day(sysdate)
-- add_months(date,number_of_months)
-------------------------------------------------------------------------------
Year
Month
Day
if any date falls on first half of the 6 month --> first_day of the year
if any date falls on second half of the year (second six months) --> first_day of the next year
if any date falls on first half of the Quarter --> first_day of the quarter
if any date falls on second half of the Quarter (second 45 days) --> first_day of the next quarter
e.g JAN FEB MAR ==> Jan 1 to Feb 14 --> first half of the Q
---------------------------------------------------------------------------------
=======================
=============================
===========================================
SELECT
FROM DUAL;
-----------------------------------------------------------------------------------
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
decode
The DECODE function in Oracle allows you to have IF-THEN-ELSE logic in your SQL statements.
The expression is the value to compare. Many combinations of search and result
can be supplied. Search is compared against the expression, and if it is true, then result is returned
City New_city
Madras Chennai
Calkatta Kolkatta
Bombay Mumbai
decode(city,'Madras','Chennai','calcatta','Kolkatta','Bombay','Mumbai','Orissa','Odissa',city) new_city
select subject_id,
decode(subject_id,1,'Mathematics',2,'Physics',3,'Chemistry','Others') subject_name
from students;
F Female
M Male
U Unknown
decode(upper(gender),'F','Female','M','Male','U','Unknown','Others')
transaction_status
S success
F FAILED
P pending
A approved
U unknown
example:
Transaction_status_code to Transaction_status:
F Failed
P - pending
U unknown
-------------------------------------------------------------------------------------------------------------
Case:
else
statement
end;
salary_status:
--------------
select employee_id,first_name,salary,
case
when salary >= 5000 and salary <15000 then 'Avg salary'
from employees;
select employee_id,first_name,salary,
case
when salary >= 5000 and salary <15000 then 'Avg salary'
case
from employees;
select count(case when salary < 5000 then 'Low salary' end ) as low_salary_count,
count(case when salary >= 5000 and salary <15000 then 'Avg salary' end ) as avg_salary_count,
count(case when salary >= 15000 then 'high salary' end ) as high_salary_count
from employees;
from customer2;
-- Exercise
'MONTH') - 1),
'SATURDAY') SECOND_SATURDAY
FROM DUAL;
========================================================