oracle的抽取函数是 extrac()
语法:
SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL; return Current Year
SELECT EXTRACT(MONTH FROM SYSDATE) FROM DUAL; return Current Month
SELECT EXTRACT(DAY FROM SYSDATE) FROM DUAL; return Current Day
SELECT EXTRACT(HOUR FROM TIMESTAMP '2010-10-10 10:10:10') FROM DUAL;
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2010-10-10 10:10:10') FROM DUAL;
例子如下:
SQL> SELECT EXTRACT(YEAR FROM SYSDATE) FROM DUAL
2 ;
EXTRACT(YEARFROMSYSDATE)
------------------------
2010
SQL> SELECT EXTRACT(month FROM SYSDATE) FROM DUAL
2 ;
EXTRACT(MONTHFROMSYSDATE)
-------------------------
3
SQL> SELECT EXTRACT(day FROM SYSDATE) FROM DUAL;
EXTRACT(DAYFROMSYSDATE)
-----------------------
3
SQL> SELECT EXTRACT(hour FROM SYSDATE) FROM DUAL;
SELECT EXTRACT(hour FROM SYSDATE) FROM DUAL
ORA-30076: 对析出来源无效的析出字段
SQL> SELECT EXTRACT(minute FROM SYSDATE) FROM DUAL;
SELECT EXTRACT(minute FROM SYSDATE) FROM DUAL
ORA-30076: 对析出来源无效的析出字段
SQL> SELECT EXTRACT(HOUR FROM TIMESTAMP '2010-10-10 10:10:10') FROM DUAL;
EXTRACT(HOURFROMTIMESTAMP'2010
------------------------------
10
SQL> SELECT EXTRACT(minute FROM TIMESTAMP '2010-10-10 10:10:10') FROM DUAL;
EXTRACT(MINUTEFROMTIMESTAMP'20
------------------------------
10
以上是EXTRACT()函数的使用说明,extract 抽取年月日部分和时分部分的使用方法是不同的.
EXTRACT(YEAR FROM SYSDATE)中FROM 的下一个参数是日期类型的时候可以抽取年月日.就是说这个参数是日期类型的时候就可以抽取年/月/日,而抽取不了时/分部分.抽取时分部分得在FROM 后面加带TIMESTAMP 且后面加时间字符
这里要注意,在(MINUTE FROM TIMESTAMP '2010-10-10 10:10:10')中,如果把'2010-10-10 10:10:10'用日期类型的字段来代替,并转换成字符类型后,也是错误的,估计是这方法的设计问题.
比如(MINUTE FROM TIMESTAMP to_date(to_char(日期类型,'YYYY-MM-DD hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss')),这样执行不了的.
例如:
SQL> select extract(MINUTE FROM TIMESTAMP to_date(to_char(sysdate,'YYYY-MM-DD hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss')) from dual;
select extract(MINUTE FROM TIMESTAMP to_date(to_char(sysdate,'YYYY-MM-DD hh24:mi:ss'),'YYYY-MM-DD hh24:mi:ss')) from dual
ORA-00907: 缺失右括号
如果要从日期类型的字段中抽取时/分部分用to_char() 函数就可以.比如to_char(sysdate,'hh24')抽取小时
例如:
抽取小时
SQL> select to_char(sysdate,'hh24') from dual;
TO_CHAR(SYSDATE,'HH24')
---------------------------------------------------------------------------
21
抽取分钟
SQL> select to_char(sysdate,'mi') from dual;
TO_CHAR(SYSDATE,'MI')
---------------------------------------------------------------------------
48
抽取秒
SQL> select to_char(sysdate,'ss') from dual;
TO_CHAR(SYSDATE,'SS')
---------------------------------------------------------------------------
42
--转自