Oracle日期處理

  • 2020 年 2 月 10 日
  • 筆記

TO_CHAR

to_char函數的功能是將數值型或者日期型轉化為字元型,這裡僅涉及其後者功能。

官方描述: TO_CHAR (datetime) converts a datetime or interval value of DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE datatype to a value of VARCHAR2 datatype in the format specified by the date format fmt. TO_CHAR (datetime)

語法

    to_char(date,'YYYY/MM/DD') 

示例

    select  to_char(sysdate, 'YYYY/MM/DD' )  FROM  DUAL;

結果

2019/04/11

日期常用格式

格式不區分大小寫,分割線可自行定義,這裡使用「/」為例:

日期格式

說明

YYYY/MM/DD

年/月/日

YYYY/MM

年/月

MM

月份

DD

日期

D

從星期日算起,一星期中的第n天。即星期日 = 1; 星期一 = 2; 星期二 = 3;星期三 = 4; 星期四 = 5; 星期五 = 6; 星期六 = 7;

DDD

一年中的第n天

WW

一年中的第n周

W

一個月中的第n周

Q

一年中的第n季度

YYYY/MM/DD HH24:MI:SS

年/月/日 時(24小時制):分:秒

YYYY/MM/DD HH:MI:SS

年/月/日 時(非24小時制):分:秒

TO_DATE

Oracle TO_DATE 函數將字元串或表達式轉換為日期值。

官方描述: TO_DATE converts char of CHAR, VARCHAR2, NCHAR, or NVARCHAR2 datatype to a value of DATE datatype. The fmt is a datetime model format specifying the format of char. If you omit fmt, then char must be in the default date format. If fmt is J, for Julian, then char must be an integer. TO_DATE

TRUNC

TRUNC(date)函數返回date當天的時間部分被格式模型fmt截斷到指定的單位

返回的值始終為數據類型DATE,即使您為該date指定了不同的datetime數據類型。如果省略fmt,則date截斷到最近的一天。

官方描述: The TRUNC (date) function returns date with the time portion of the day truncated to the unit specified by the format model fmt. The value returned is always of datatype DATE, even if you specify a different datetime datatype for date. If you omit fmt, then date is truncated to the nearest day. TRUNC (date)

示例

select trunc(sysdate)         from dual  --2019-04-11 00:00:00  今天的日期為2019-04-11  select trunc(sysdate, 'mm')   from dual  --2019-04-01 00:00:00  返回當月第一天.  select trunc(sysdate,'yy')    from dual  --2019-01-01 00:00:00  返回當年第一天  select trunc(sysdate,'dd')    from dual  --2019-04-11 00:00:00  返回當前年月日  select trunc(sysdate,'yyyy')  from dual  --2019-01-01 00:00:00  返回當年第一天  select trunc(sysdate,'d')     from dual  --2019-04-07 00:00:00  返回當前星期的第一天 (星期天為星期的第一天)  select trunc(sysdate, 'hh')   from dual  --2019-04-11 19:00:00  當前時間為19:20  select trunc(sysdate, 'mi')   from dual  --2019-04-11 19:16:00  精確到分鐘,TRUNC()函數沒有秒的精度

EXTRACT

EXTRACT從日期時間或間隔值表達式中提取並返回指定日期時間欄位的值。

官方描述: EXTRACT extracts and returns the value of a specified datetime field from a datetime or interval value expression. When you extract a TIMEZONE_REGION or TIMEZONE_ABBR (abbreviation), the value returned is a string containing the appropriate time zone name or abbreviation. When you extract any of the other values, the value returned is in the Gregorian calendar. When extracting from a datetime with a time zone value, the value returned is in UTC. For a listing of time zone names and their corresponding abbreviations, query the V$TIMEZONE_NAMES dynamic performance view. EXTRACT (datetime)

    extract(expression   from   date) 

示例

    select extract(year from sysdate) FROM  DUAL;

結果

2019

條件expression說明

expression

說明

year

年度

month

月份

day

日期

參考

Oracle Database Online Documentation10g Release 2 (10.2)

ORACLE時間欄位取年、月、日、季度

oracle時間函數(包括截取時間)

Oracle 查詢時間在當天的數據