SQL between and 日期范围 筛选数据不符

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL between and 日期范围 筛选数据不符相关的知识,希望对你有一定的参考价值。

表中数据 2011-7-13 16:02:46 类型是nvarchar(50)A:select * from table where [time] between '2011-7-1 00:00:00' and '2010-7-14 23:59:59' B:select * from table where [time] between '2011-7-2 00:00:00' and '2010-7-14 23:59:59' C:select * from table where [time] between '2011-7-9 00:00:00' and '2010-7-14 23:59:59' D:select * from table where [time] between '2011-7-10 00:00:00' and '2010-7-14 23:59:59' E:select * from table where [time] between '2011-7-13 00:00:00' and '2010-7-14 23:59:59' 理论应该是全部都可以查出来的但是事实上却是只有ADE可以所以我就怀疑是字符串的的比较造成的,原因如下:BC语句是2-9日,比表中的13日的“1”大现在求代码解决方案,是2-9日变成02-09日,还是说怎么转格式来比较

建议加上0,如2013-02-09,在某些情况下,字符串向日期的自动转换会被服务器操作系统的日期格式受影响 参考技术A select * from table where cast([time] as datetime), between '2011-7-2 00:00:00' and '2010-7-14 23:59:59' 这样看看行不

以上是关于SQL between and 日期范围 筛选数据不符的主要内容,如果未能解决你的问题,请参考以下文章

SQL中 between and 包不包括两端值

SQL语句中 between and

SQL数据库怎么查询,between 当前日期 and 当前日期前30天,之间的数据

【SQL语句】-范围查询

关于sql语句between and的边界问题

SQL语句怎样查询一个范围