ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 如何提取oracle timestamp列一个范围的数据

如何提取oracle timestamp列一个范围的数据

原创 Linux操作系统 作者:wisdomone1 时间:2013-02-28 16:49:22 0 删除 编辑

--创建测试表
SQL> create table t_timestamp(a timestamp);
 
Table created

--插入几条测试数据
SQL> insert into t_timestamp values(systimestamp-1);
 
1 row inserted
 
SQL> insert into t_timestamp values(systimestamp);
 
1 row inserted
 
SQL> insert into t_timestamp values(systimestamp+1);
 
1 row inserted
 
SQL> commit;
 
Commit complete
--查看测试数据
SQL> select * from t_timestamp;
 
A
--------------------------------------------------------------------------------
27-FEB-13 04.34.54.000000 PM
28-FEB-13 04.35.00.641000 PM
01-MAR-13 04.35.04.000000 PM
--查看与timestamp相关初始化参数配置
SQL> show parameter timestamp
 
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
nls_timestamp_format                 string     
nls_timestamp_tz_format              string     
--配置当前会话的timestamp格式
SQL> alter session set nls_timestamp_format='yyyy-mm-dd hh24:mi:ss.ff';
 
Session altered
--查询测试表的timestamp格式是否如愿显示
SQL> select * from t_timestamp;
 
A
--------------------------------------------------------------------------------
2013-02-27 16:34:54.000000
2013-02-28 16:35:00.641000
2013-03-01 16:35:04.000000
 
--trunc可加工timestamp,经加工的数据类型为date
SQL> select trunc(a),a from t_timestamp;
 
TRUNC(A)    A
----------- --------------------------------------------------------------------------------
2013/2/27   2013-02-27 16:34:54.000000
2013/2/28   2013-02-28 16:35:00.641000
2013/3/1    2013-03-01 16:35:04.000000
 
--对trunc后的数据以to_char格式转换
SQL> select trunc(a),to_char(trunc(a),'yyyy-mm-dd'),a from t_timestamp;
 
TRUNC(A)    TO_CHAR(TRUNC(A),'YYYY-MM-DD') A
----------- ------------------------------ --------------------------------------------------------------------------------
2013/2/27   2013-02-27                     2013-02-27 16:34:54.000000
2013/2/28   2013-02-28                     2013-02-28 16:35:00.641000
2013/3/1    2013-03-01                     2013-03-01 16:35:04.000000
 
--使用to_timestamp及接接字符串提取2013-02-27及2013-02-28两天所有的数据
SQL> select a from t_timestamp where a between to_timestamp('2013-02-27'||'00:00:00.000000','yyyy-mm-dd hh24:mi:ss.ff') and to_timestamp('2013-02-28'||'23:59:59.000000','yyyy-mm-dd hh24:mi:ss.ff');
 
A
--------------------------------------------------------------------------------
2013-02-27 16:34:54.000000
2013-02-28 16:35:00.641000

--显式配置nls_timestamp_format后to_timestamp不用指定格式字符串
SQL> select a from t_timestamp where a between to_timestamp('2013-02-27'||'00:00:00.000000') and to_timestamp('2013-02-28'||'23:59:59.000000');
 
A
--------------------------------------------------------------------------------
2013-02-27 16:34:54.000000
2013-02-28 16:35:00.641000

小结:

        1,to_timestamp使用

        2,拼接字符串

        3,或与不或配置nls_timestamp_format(非必选项)

        4,确定时间范围的起始点:即最先的时间点及最末的时间点

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/9240380/viewspace-754979/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
提供针对oracle初学者及进阶的数据库培训,欢迎大家咨询: 微信: wisdomone 微信公众号: lovedb 工作经历: 中国普天 北京科蓝 北京云和恩墨 北京神州新桥

注册时间:2008-04-04

  • 博文量
    2189
  • 访问量
    12686099