日期:2014-05-17  浏览次数:20692 次

to_char,to_date问题
现在表里有个字段BEGIN_TIME是DATE类型,(date跟varchar有什么区别),然后记录形式如下
2007-1-19   15:22:47

现在要查找这个月里1月5号13:00到17:00的记录

select   *   from   test   where   to_???????????????????????????????????

------解决方案--------------------
上面的错了
日期格式应改为hh24:mi:ss,语句如下
select * from test where BEGIN_TIME between to_date( '2007-01-19 13:00:00 ', 'YYYY-MM-DD hh24:mi:ss ') and to_date( '2007-01-05 17:00:00 ', 'YYYY-MM-DD hh24:mi:ss ')
------解决方案--------------------
BEGIN_TIME是DATE类型
date 类型的数据是不能存到时间的 只能存到日期
比如 2007-1-19 15:22:47 在数据库里面存的数值只是 2007-01-19

所以 按照你的查询条件 只需要这么写
select * from test where BEGIN_TIME between to_date( '2007-01-19 ', 'YYYY-MM-DD ') and to_date( '2007-01-05 ', 'YYYY-MM-DD '); 就应该可以了


------解决方案--------------------
Paladin_china(大仙) 的回复大体是正确的,但是FORMAT格式有问题,应是:
select * from test where BEGIN_TIME between to_date( '2007-01-19 13:00:00 ', 'YYYY-MM-DD HH24:MI:SS ') and to_date( '2007-01-05 17:00:00 ', 'YYYY-MM-DD HH24:MI:SS ');

------解决方案--------------------
to_char(date, 'yyyy-mm-dd ')

to_date(char, 'yyyy-mm-dd ')