sql按时间查询 我现在有一张表 里面有一个时间字段 类型是vachar2(20) ,如何按时间段进行数据的查询,这样的SQL如何写? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 直接比较,输入的如果是date类型,则将输入参数用to_char函数转化成同字段格式的数据进行比较 里面存的数据为2009-10-09 12:10:30 方便的话把sql 写一些吧 谢谢 SQL> create table t1(time varchar2(20));Table created.SQL> insert into t1 values('2009-11-17 12:10:20');1 row created.SQL> insert into t1 values('2009-11-16 12:10:20');1 row created.SQL> insert into t1 values('2009-11-16 15:10:20');1 row created.SQL> commit;Commit complete.SQL> select * from t1;TIME--------------------2009-11-17 12:10:202009-11-16 12:10:202009-11-16 15:10:20SQL> select * from t1 2 where to_date(time,'YYYY-MM-DD HH24:MI:SS') between to_date('2009-11-16 13:00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2009-11-17 13:00:00','YYYY-MM-DD HH24:MI:SS');TIME--------------------2009-11-17 12:10:202009-11-16 15:10:20 e ,楼上已经写了直接字符串比较应该也是可以的select * from t1where time between '2009-11-16 13:00:00' and '2009-11-17 13:00:00'; 恩 我刚试了下 直接比字符串也是可以的呵呵 ORACLE还挺聪明哦 把字符串当日期比 我试了几个特殊值都没试出错误 好像是可以不过有个限制条件 那就是比较字符串必须与表中存储日期字符串的格式相同才行SQL> select * from t1;TIME--------------------2009-11-17 12:10:202009-11-16 12:10:202009-11-16 15:10:20SQL> select * from t1 2 where time between '09-11-16 13: 3 00:00' and '09-11-17 13:00:00';no rows selectedSQL> select * from t1 2 where time between '2009-11-16 13: 3 00:00' and '2009-11-17 13:00:00';TIME--------------------2009-11-17 12:10:202009-11-16 15:10:20 提两人关于数据库口令认证的问题。 请教比对字符串的问题? 如何更改oracle8i的配置 存储过程中as 与 is 关键字的区别? 求光纤网络中ORACLE问题的解决方法 ***这个sql怎么写?*** 日期格式 我装了一个TOAD7.3,里面有个SQLAB TUNING却因没有注册用不成,谁有SQLAB这个的注册码? 一个简单的sql文问题 oracle表的主键为什么不能用Number类型? Enterprise Manager console 打开非本机的oracle数据库 oracle dblink小问题
--------------------
2009-11-17 12:10:20
2009-11-16 12:10:20
2009-11-16 15:10:20
SQL> select * from t1
2 where to_date(time,'YYYY-MM-DD HH24:MI:SS') between to_date('2009-11-16 13:
00:00','YYYY-MM-DD HH24:MI:SS') and to_date('2009-11-17 13:00:00','YYYY-MM-DD HH
24:MI:SS');TIME
--------------------
2009-11-17 12:10:20
2009-11-16 15:10:20
select * from t1
where time between '2009-11-16 13:
00:00' and '2009-11-17 13:00:00';
呵呵 ORACLE还挺聪明哦 把字符串当日期比 我试了几个特殊值都没试出错误 好像是可以
不过有个限制条件 那就是比较字符串必须与表中存储日期字符串的格式相同才行SQL> select * from t1;TIME
--------------------
2009-11-17 12:10:20
2009-11-16 12:10:20
2009-11-16 15:10:20SQL> select * from t1
2 where time between '09-11-16 13:
3 00:00' and '09-11-17 13:00:00';no rows selectedSQL> select * from t1
2 where time between '2009-11-16 13:
3 00:00' and '2009-11-17 13:00:00';TIME
--------------------
2009-11-17 12:10:20
2009-11-16 15:10:20