oracle Extract 函数

  1. //oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分
  2. //语法如下:
  3. EXTRACT (
  4. { YEAR | MONTH | DAY | HOUR | MINUTE | SECOND }
  5. | { TIMEZONE_HOUR | TIMEZONE_MINUTE }
  6. | { TIMEZONE_REGION | TIMEZONE_ABBR }
  7. FROM { date_value | interval_value } )
  8. //我们只可以从一个date类型中截取 year,month,day(date日期的格式为yyyy-mm-dd);
  9. //我们只可以从一个 timestamp with time zone 的数据类型中截取TIMEZONE_HOUR和TIMEZONE_MINUTE;
  10. select extract(year from date'2011-05-17') year from dual;
  11. YEAR
  12. ----------
  13. 2011
  14. select extract(month from date'2011-05-17') month from dual;
  15. MONTH
  16. ----------
  17. 5
  18. select extract(day from date'2011-05-17') day from dual;
  19. DAY
  20. ----------
  21. 17
  22. //获取两个日期之间的具体时间间隔,extract函数是最好的选择
  23. select extract(day from dt2-dt1) day
  24. ,extract(hour from dt2-dt1) hour
  25. ,extract(minute from dt2-dt1) minute
  26. ,extract(second from dt2-dt1) second
  27. from (
  28. select to_timestamp('2011-02-04 15:07:00','yyyy-mm-dd hh24:mi:ss') dt1
  29. ,to_timestamp('2011-05-17 19:08:46','yyyy-mm-dd hh24:mi:ss') dt2
  30. from dual)
  31. /
  32. DAY HOUR MINUTE SECOND
  33. ---------- ---------- ---------- ----------
  34. 102 4 1 46
  35. --
  36. select extract(year from systimestamp) year
  37. ,extract(month from systimestamp) month
  38. ,extract(day from systimestamp) day
  39. ,extract(minute from systimestamp) minute
  40. ,extract(second from systimestamp) second
  41. ,extract(timezone_hour from systimestamp) th
  42. ,extract(timezone_minute from systimestamp) tm
  43. ,extract(timezone_region from systimestamp) tr
  44. ,extract(timezone_abbr from systimestamp) ta
  45. from dual
  46. /
  47. YEAR MONTH DAY MINUTE SECOND TH TM TR TA
  48. ---------- ---------- ---------- ---------- ---------- ---------- ---------- --------- ----------
  49. 2011 5 17 7 14.843 8 0 UNKNOWN UNK
  50. //

你可能感兴趣的:(oracle Extract 函数)