日期:2014-05-18  浏览次数:20609 次

SQL语句问题,怎么查结果都一样
select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime)) between 1 and 5




select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-19' as datetime)) between 1 and 5



select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime)) between 1 and 10


怎么查结果都一样 结果都是数据表里的全部数据 没有 按指定时间显示

哪里的问题?

数据库的 日期字段 为字符串型的 vchar



------解决方案--------------------
SQL code

select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime)) between 1 and 5

--datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime))=3,3在1和5之间,条件为真,但会所有结果
和


select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-19' as datetime)) between 1 and 5
 --datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-19' as datetime))=7,不在1和5之间,返回空集
和

select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime)) between 1 and 10
--datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime))=3,条件成立,返回所有结果

------解决方案--------------------
探讨

如果用
SQL code



select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),getdate()) between 1 and 5




不提示错误 但会没有结果

------解决方案--------------------
SQL code

select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),getdate()) between 1 and 5

--datediff(dd,cast('2012-04-12' as datetime),getdate())值为17
--不在1到5的范围内,条件不成立,所以表里面的内容查不出来

------解决方案--------------------
探讨
select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,cast('2012-04-12' as datetime),cast('2012-04-15' as datetime)) between 1 and 5




select *
from DQ_Rp_Plan_Houseitem
where datediff(dd,c……