Oracle 字串和日期轉換

2022-09-10 03:21:09 字數 3289 閱讀 5418

yyyy                 完整年

yyy 完整年後3位數

yy 完整年後2位數

y 完整年最1位數

month

月_全稱

mm 當年第幾月

ddd 當年第幾天

dd 當月第幾天

day星期幾_全稱

d 星期幾_每週從禮拜天開始_1_7

hh 當前小時_12小時制

hh24 當前小時_24小時制

mi 分鐘

ss 秒

q 季度_1_4

ww 年中的第幾個星期

w 月中的第幾個星期

demosql

select

'2022-02-01

'itemdate

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

yyyy

') yyyy

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

yyy'

) 年後3位數

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'yy'

) 年後2位數

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'y'

) 年後1位數

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

month

') 月_全稱

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'mm'

) 當年第幾月

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

ddd'

) 當年第幾天

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'dd'

) 當月第幾天

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

day'

) 星期幾_全稱

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'d'

) 星期幾_每週從禮拜天開始_1_7

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'hh'

) 當前小時_12小時制

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'

hh24

') 當前小時_24小時制

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'mi'

) 分鐘

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'ss'

) 秒 , to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'q'

) 季度_1_4

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'ww'

) 年中的第幾個星期

, to_char(to_date(

'2022-02-01

','yyyy-mm-dd

'),'w'

) 月中的第幾個星期

from dual;

view code

/*

** str: 日期字串

* format: 日期字串的時間格式,需要與str匹配,或者會報錯

*/select to_date('

2022-02-01

','yyyy-mm-dd

') from dual; --

okselect to_date('

2022-02-01

','yyyy-mm-dd hh24:mi:ss

') from dual; --

okselect to_date('

2022-02-01 13:14:00

','yyyy-mm-dd

') from dual; --

error-ora-01830: 日期格式在轉換整個輸入字串之前結束

/*

** date: 日期

* format: 日期的字串格式,預設格式為 'dd-month-yy',需要與date匹配,或者會報錯

*/select to_char(sysdate) from dual; --

okselect to_char(sysdate,'

yyyy-mm-dd

') from dual; --

okselect to_char(sysdate,'

yyyy-mm-dd hh24:mi:ss

') from dual; --

okselect to_char(sysdate,'

yyyy-mm-dd hh24:mi:ss1

') from dual; --

error-ora-01821: 日期格式無法識別

日期和字串互相轉換

1.日期與字串的轉換 select to char sysdate,yyyy mm dd hh24 mi ss as nowtime from dual 日期轉化為字串 select to date 2004 05 07 13 23 44 yyyy mm dd hh24 mi ss from dua...

日期Date和字串之間轉換

date string 格式化 public final string format date date string date 解析 public date parse string source dateforamt 可以進行日期和字串的格式化和解析,但是由於是抽象類,所以使用具體子類 date...

日期字串格式轉換

日期格式化轉換,是開發過程中比較常遇到的問題。例如以下格式的日期 13.06.2017 2017 06 13 2017年06月13日 2017年6月13日。針對固定格式的日期可以用字串擷取的方式格式化成需要的格式,但是不夠通用。擴充套件方法將日期從原始格式轉化成需要的格式 public static...