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

SQL语句有关问题,如何查结果都一样

2012-05-11 
SQL语句问题,怎么查结果都一样select *from DQ_Rp_Plan_Houseitemwhere datediff(dd,cast(2012-04-12 as

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_Houseitemwhere 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_Houseitemwhere 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_Houseitemwhere 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_Houseitemwhere 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……

热点排行