首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

sql 按时间段查询,该怎么处理

2012-01-20 
sql 按时间段查询出现比较奇怪的现象,查询语句 select *from table where period between 2011-1and 20

sql 按时间段查询
出现比较奇怪的现象,
查询语句 select *from table where period between '2011-1'and '2011-10'
实际内容的时间在 2011-1 到2011-5 之间,但是只查询出来2011-1的,
如果换成时间段在between 2011-1 and 2011-9 又能全部查出来
就是2011-10,2011-11,2011-12 查询会失败,这是什么原因,哪位大侠能帮我解释一下,谢谢!

[解决办法]
你的period应该是字符串类型
字符串比较大小是按从左到右,每一位来比较的
就像'20'首位是2,比'1000'首位是1大
所以'20'>'1000'

用字符串存日期类型,注意将月份、日期补0
这样'2011-01'就可以和'2011-10'比较了
要么转为日期类型再比较
 to_date(period,'yyyy-m') between to_date('2011-1','yyyy-m') and to_date('2011-10','yyyy-m')
 
[解决办法]

SQL code
select *from table where period between to_date('2011-01', 'YYYY-MM')and to_date('2011-10', 'YYYY-MM');
[解决办法]
to_date转换一下类型吧
[解决办法]
探讨
SQL code

select *from table where period between to_date('2011-01', 'YYYY-MM')and to_date('2011-10', 'YYYY-MM');

热点排行