Oracle Database 提取日期的年,月,日,小时,分钟或秒部分
示例
DATE数据类型的年,月或日组成部分可以使用EXTRACT([YEAR|MONTH|DAY]FROMdatevalue)
SELECT EXTRACT (YEAR FROM DATE '2016-07-25') AS YEAR, EXTRACT (MONTH FROM DATE '2016-07-25') AS MONTH, EXTRACT (DAY FROM DATE '2016-07-25') AS DAY FROM DUAL;
输出:
YEAR MONTH DAY ---- ----- --- 2016 7 25
时间(小时,分钟或秒)部分可以通过以下任一方式找到:
使用CAST(datevalueASTIMESTAMP)将转换DATE为aTIMESTAMP,然后使用EXTRACT([HOUR|MINUTE|SECOND]FROMtimestampvalue);要么
使用TO_CHAR(datevalue,format_model)来获取值作为一个字符串。
例如:
SELECT EXTRACT( HOUR FROM CAST( datetime AS TIMESTAMP ) ) AS Hours, EXTRACT( MINUTE FROM CAST( datetime AS TIMESTAMP ) ) AS Minutes, EXTRACT( SECOND FROM CAST( datetime AS TIMESTAMP ) ) AS Seconds FROM ( SELECT TO_DATE( '2016-01-01 09:42:01', 'YYYY-MM-DD HH24:MI:SS' ) AS datetime FROM DUAL );
输出:
HOURS MINUTES SECONDS ----- ------- ------- 9 42 1