`

oracle根据时间查询

阅读更多


oracle根据时间查询主要有两个函数(
1、to_char(将时间类型转换成char类型);
2、to_date(将String类型转换成date类型);
例如,
1)表名:test_table,字段名:addtime,查询2012年3月12日的记录
select * from test_table t where to_char(t.addtime,'yyyy-MM-dd')='2012-03-12';
注:这个时候使用select * from test_table t where t.addtime=to_date('2012-03-12','yyyy-MM-dd')是不行的,because:数据库存储date类型的字段,会保存时分秒的;

2)表名:test_table,字段名:addtime,查询2012年3月12日0时0分0秒的记录:
select * from test_table t where to_char(t.addtime,'yyyy-MM-dd hh24:mi:ss')='2012-03-12 00:00:00';
或者:select * from test_table t where t.addtime=to_date('2012-03-12 00:00:00','yyyy-MM-dd hh24:mi:ss');

3)表名:test_table,字段名:addtime,查询2012年3月12日到2012年3月15日的记录:
select * from test_table t where to_char(t.addtime,'yyyy-MM-dd') between '2012-03-12' and '2012-03-15';

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics