Oracle Date / Time Function

 -- sysdate 


SELECT sysdate from dual

SYSDATE
2022/02/22


-- systimestamp 


SELECT systimestamp from dual


SYSTIMESTAMP
22-2月 -22 01.28.54.849788 上午 +00:00



-- add_months 
SELECT add_months('2022/02/22', 3)
from dual

ADD_MONTHS('2022/02/22',3)
2022/05/22


SELECT add_months('2022/02/22', -3)
from dual
ADD_MONTHS('2022/02/22',-3)
2021/11/22



-- months_between 

SELECT months_between('2022/02/15', '2023/02/15')
from dual
MONTHS_BETWEEN('2022/02/15','2023/02/15')
-12


--加入trunc

SELECT trunc(systimestamp) from dual
TRUNC(SYSTIMESTAMP)
2022/02/22


SELECT trunc(systimestamp, 'YEAR') from dual
TRUNC(SYSTIMESTAMP,'YEAR')
2022/01/01



SELECT trunc(systimestamp, 'MONTH') from dual
TRUNC(SYSTIMESTAMP,'MONTH')
2022/02/01

Oracle ROUND TRUNC

ROUND:選擇小數點後要保留幾位數並四捨五入

SELECT round(107.088, 2) from dual
ROUND(107.088,2)
107.09

TRUNC:依第二個參數來決定返回的位數

TRUNC( number [, decimal_places] )

SELECT trunc(107.938439849, 3) from dual

TRUNC(107.938439849,3)
107.938

Oracle character based SRF

 INITCAP:英文單字中第一個字轉大寫

query:

SELECT INITCAP('hello my name is Imtiaz') AS sentence

FROM dual


SENTENCE
Hello My Name Is Imtiaz


LENGTH:計算字串長度

query:

SELECT ename, LENGTH(ename) as length
FROM emp
where LENGTH(ename) = 6

ENAMELENGTH
MARTIN6
TURNER6
MILLER6


SUBSTR:切割字串
substr(string, start_position, [length])
依照參數分別帶入 字串 起始位置 要取幾個字
如果後面的length沒有提供的話就是從起始位置開始

query:
SELECT 'hello', SUBSTR('hello', 2, 2)
FROM dual

'HELLO'SUBSTR('HELLO',2,2)
helloel


LPAD:向左補滿位數
LPAD( string1, padded_length [, pad_string] )
參數分別是 字串 顯示長度 要補的字
例如想要在hello左邊補&並讓整個字串長度為10就可以這樣寫
query:

SELECT LPAD('hello', 10, '&')
FROM dual
LPAD('HELLO',10,'&')
&&&&&hello


RPAD:向右補滿位數


LTRIM:
LTRIM( string1 , [trim_string] ) 

RTRIM:

RTRIM( string1 [, trim_string ] )
參數分別是字串 要去除的內容
query:

SELECT LTRIM('hello', 'h')
FROM dual

LTRIM('HELLO','HAC')
ello

Oracle SRF & using dual table

之前學過用SELECT字串加上 || column 來組合查詢結果

SELECT 'Hi ! ' || ename AS HI

FROM emp


也可以用CONCAT來達成同樣的效果

SELECT CONCAT('Hi ! ', ename) AS HI

FROM emp


HI
Hi !KING
Hi !BLAKE
Hi !CLARK
Hi !JONES
Hi !SCOTT
Hi !FORD
Hi !SMITH
Hi !ALLEN
Hi !WARD
Hi !MARTIN




假如我們輸入以下query


SELECT UPPER('hello')
FROM emp
WHERE deptno = 20

這樣就只會有符合條件的資料會變成大寫HELLO

UPPER('HELLO')
HELLO
HELLO
HELLO
HELLO
HELLO


DUAL表

簡單來說,dual表就是oracle與數據字典自動創建的一張表,這張表是一個單行單列的表,這個表只有1列:DUMMY,數據類型為VERCHAR2(1),dual表中只有一個數據'X', Oracle有內部邏輯保證dual表中永遠只有一條數據。dual表主要是用來選擇系統變量或是求一個表達式的值。
比如:
--求系統當前時間
SELECT sysdate FROM daul
--求系統當前時間,並按設定的格式顯示
select to_char(sysdate,''yyyy-mm-dd hh24:mi:ss'') from dual;
--當計算器使用
select 1+2 from dual


除了upper把字都轉大寫以外當然也要有把字轉小寫的功能
這就要透過lower來完成
query:
select LOWER('hello')
from DUAL 

LOWER('HELLO')
hello



練習query
SELECT
    CONCAT(CONCAT(LOWER(ename), UPPER(' is the name')), CONCAT(Lower(' and their job is : '), UPPER(job))) AS sentence
FROM
    emp
WHERE
    deptno = 20

SENTENCE
jones IS THE NAME and their job is : MANAGER
scott IS THE NAME and their job is : ANALYST
ford IS THE NAME and their job is : ANALYST
smith IS THE NAME and their job is : CLERK
adams IS THE NAME and their job is : CLERK

Oracle quiz1

 



Q1.Write a query that retrieves suppliers that work in either Georgia or California.

SELECT * 
FROM supplier 
WHERE state in ('Georgia', 'California')

Q2.Write a query that retrieves suppliers with the characters "wo" and the character "I" or "i" in their name.

SELECT 
FROM supplier 
WHERE supplier_name LIKE '%wo%' 
AND ( supplier_name LIKE '%I%' OR supplier_name LIKE '%i%' )

Q3.Write a query that retrieves suppliers on which a minimum of 37,000 and a maximum of 80,000 was spent.

SELECT 
FROM supplier 
WHERE total_spent 
BETWEEN 37000 AND 80000

Q4.Write a query that returns the supplier names and the state in which they operate meeting the following conditions:

belong in the state Georgia or Alaska
the supplier id is 100 or greater than 600
the amount spent is less than 100,000 or the amount spent is 220,000
SELECT supplier_name, state
FROM supplier
WHERE state IN('Georgia', 'Alaska')
AND (supplier_id = 100 OR supplier_id > 600)
AND(total_spent < 100000 OR total_spent = 220000)


Q5.TRUE or FALSE Question:
The keywords such as SELECT and WHERE must always be capital in the SQL Query.

F

Q6.TRUE or FALSE Question:
The database works on first processing the filtering conditions and then processes the FROM condition.

F

Q7.TRUE or FALSE Question:
Having just the filter condition shown below in a SQL query will return all of the records from the table.
WHERE 1 = 1
T

Q8.TRUE or FALSE question:
NULL can not be compared using an equal sign.

T

Q9.TRUE or FALSE question:
The ORDER BY clause is processed before the FROM clause in a SQL statement and it's used to sort the columns in an ascending or descending fashion.

F

Oracle ORDER BY

 我們在尋找資料的同時也能用 ORDER BY 來排序資料


例如依照sal的值

query:

SELECT *

FROM emp

ORDER BY sal


EMPNOENAMEJOBMGRHIREDATESALCOMMDEPTNO
7369SMITHCLERK79021980/12/17800-20
7900JAMESCLERK76981981/12/03950-30
7876ADAMSCLERK77881983/01/121100-20
7654MARTINSALESMAN76981981/09/281250140030
7521WARDSALESMAN76981981/02/22125050030
7934MILLERCLERK77821982/01/231300-10
7844TURNERSALESMAN76981981/09/081500030
7499ALLENSALESMAN76981981/02/20160030030
7782CLARKMANAGER78391981/06/092450-10
7698BLAKEMANAGER78391981/05/012850-30


加入DESC也可以降冪排列

query:

SELECT *

FROM emp

ORDER BY sal DESC


加入ASC是升冪排列

SELECT *

FROM emp

ORDER BY SAL ASC



ORDER BY 也可以使用多個column

SELECT DEPTNO, SAL, ENAME

FROM EMP

ORDER BY DEPTNO, SAL