oracle Extract,to_char 函数

简介: //oracle中extract()函数从oracle 9i中引入,用于从一个date或者interval类型中截取到特定的部分  //语法如下:  EXTRACT...
//oracle中extract()函数从oracle 9i中引入,用于从一个 date 或者interval类型中截取到特定的部分  
//语法如下:  
EXTRACT (  
         { YEAR  | MONTH  | DAY  | HOUR  | MINUTE  | SECOND  }  
         | { TIMEZONE_HOUR | TIMEZONE_MINUTE }  
         | { TIMEZONE_REGION | TIMEZONE_ABBR }  
FROM  { date_value | interval_value } )  
//我们只可以从一个 date 类型中截取 year , month , day ( date 日期的格式为yyyy-mm-dd);  
//我们只可以从一个 timestamp  with  time  zone 的数据类型中截取TIMEZONE_HOUR和TIMEZONE_MINUTE;  
select  extract( year  from  date '2011-05-17' ) year  from  dual;  
       YEAR  
----------  
       2011  
select  extract( month  from  date '2011-05-17' ) month  from  dual;  
      MONTH  
----------  
          5  
select  extract( day  from  date '2011-05-17' ) day  from  dual;  
        DAY  
----------  
         17  
//获取两个日期之间的具体时间间隔,extract函数是最好的选择  
select  extract( day  from  dt2-dt1) day  
       ,extract( hour  from  dt2-dt1) hour  
       ,extract( minute  from  dt2-dt1) minute  
       ,extract( second  from  dt2-dt1) second  
from  (  
      select  to_timestamp( '2011-02-04 15:07:00' , 'yyyy-mm-dd hh24:mi:ss' ) dt1  
            ,to_timestamp( '2011-05-17 19:08:46' , 'yyyy-mm-dd hh24:mi:ss' ) dt2  
      from  dual)  
/  
        DAY        HOUR      MINUTE      SECOND  
---------- ---------- ---------- ----------  
        102          4          1         46  
--  
select  extract( year  from  systimestamp) year  
       ,extract( month  from  systimestamp) month  
       ,extract( day  from  systimestamp) day  
       ,extract( minute  from  systimestamp) minute  
       ,extract( second  from  systimestamp) second  
       ,extract(timezone_hour from  systimestamp) th  
       ,extract(timezone_minute from  systimestamp) tm  
       ,extract(timezone_region from  systimestamp) tr  
       ,extract(timezone_abbr from  systimestamp) ta  
from  dual  
/  
      YEAR       MONTH         DAY      MINUTE      SECOND          TH         TM TR         TA  
---------- ---------- ---------- ---------- ---------- ---------- ---------- --------- ----------  
       2011          5         17          7     14.843          8          0 UNKNOWN   UNK  





to_char(timestamp, text) text 把 timestamp 转换成 string to_char(timestamp 'now','HH12:MI:SS')
to_char(int, text) text 把 int4/int8 转换成 string to_char(125, '999')
to_char(float, text) text 把 float4/float8 转换成 string to_char(125.8, '999D9')
to_char(numeric, text) text 把 numeric 转换成 string to_char(numeric '-125.8', '999D99S')
to_date(text, text) date 把 string 转换成 date to_date('05 Dec 2000', 'DD Mon YYYY')
to_timestamp(text, text) date 把 string 转换成 timestamp to_timestamp('05 Dec 2000', 'DD Mon YYYY')
to_number(text, text) numeric 把 string 转换成 numeric to_number('12,454.8-', '99G999D9S')

目录
相关文章
|
4月前
|
SQL Oracle 关系型数据库
Oracle之日期计算相关函数
Oracle之日期计算相关函数
45 0
|
4月前
|
SQL Oracle 关系型数据库
Oracle之regexp系列函数详解
Oracle之regexp系列函数详解
145 1
|
7月前
|
存储 SQL Oracle
Oracle数据库批量删除表、视图、序列、存储过程、函数脚本
Oracle数据库批量删除表、视图、序列、存储过程、函数脚本
68 0
|
3月前
|
存储 Java 数据库
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(二)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
38 0
|
3月前
|
SQL Oracle 关系型数据库
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
66 0
JAVAEE框架数据库技术之12_oracle常用函数和高级查询子查询
|
7天前
|
Oracle 算法 关系型数据库
Oracle常用系统函数之数字类函数:数字的魔术师
【4月更文挑战第19天】Oracle数据库中的数字类函数是数字处理的魔术师,包括`ROUND`(四舍五入),`CEIL`和`FLOOR`(向上/下取整),以及`ABS`(计算绝对值)。还有`MOD`、`TRUNC`和`POWER`等函数,提供求余数、截断和计算幂的功能。熟练运用这些函数能提升数据管理效率,让处理数字变得更简单、有趣。
|
2月前
|
SQL Oracle 关系型数据库
Oracle查询优化-聚集函数
【2月更文挑战第5天】【2月更文挑战第13篇】聚集函数
19 4
|
3月前
|
存储 SQL Java
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数(一)
JAVAEE框架数据库技术之13_oracle 之PLSQL技术及存储过程和函数
35 0
|
3月前
|
SQL Oracle 算法
Oracle函数
Oracle函数
98 1
|
4月前
|
SQL Oracle 关系型数据库
Oracle之有哪些日期计算函数?
Oracle之有哪些日期计算函数?
114 0