T-SQL 查询返回不应该返回的项目
这是场景。有问题的列称为“datein”,其类型为“datetime”。我有三行“datein”的值为“2009-10-01 00:00:00.000”。为什么这个查询返回上述行?
SELECT *
FROM t_call AS tc
WHERE tc.datein >= '2009-09-30 00:00:00.000'
AND tc.datein <= '2009-09-30 23:59:59.999'
使用
SELECT *
FROM t_call AS tc
WHERE tc.datein BETWEEN '2009-09-30 00:00:00.000'
AND '2009-09-30 23:59:59.999'
返回相同的结果
Here's the scenario. The column in question is called 'datein' and it's type is 'datetime'. I have three rows with the value of '2009-10-01 00:00:00.000' for 'datein'. Why does this query return the aforementioned rows?
SELECT *
FROM t_call AS tc
WHERE tc.datein >= '2009-09-30 00:00:00.000'
AND tc.datein <= '2009-09-30 23:59:59.999'
Using
SELECT *
FROM t_call AS tc
WHERE tc.datein BETWEEN '2009-09-30 00:00:00.000'
AND '2009-09-30 23:59:59.999'
returns the same result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
列类型是什么?如果它是日期时间,请尝试将要比较的值也转换为日期时间;如果它是字符串(char、nchar、varchar、nvarchar),您对该列使用什么排序规则等?
What is the column type? If it is a datetime, try casting the values you're comparing with to a datetime as well; if it is a string (char, nchar, varchar, nvarchar) what collation etc. are you using for the column?
根据 MSDN 文档
根据该页面中给定的示例,您必须以 .997 结束查询才能获得您期望的结果。
According to MSDN documentation
From the given example in that page, you must end your query with .997 to obtain the results that you expect.
编写这些查询的安全方法如下:
The safe way to write these queries is as follows:
这是缺乏千分之一秒值的精度。请尝试使用“.997”。
MSDN DateTime 文档
运行此命令,您将看到:
It's the lack of precision in the thousandths of a second value. Try ".997" instead.
MSDN DateTime documentation
Run this and you'll see:
DATETIME 精度 为 0.00333 秒。因此,您需要转到“2009-09-30 23:59:59.998”,这样它就不会四舍五入到 10 月 1 日。
例如:
返回:
The DATETIME accuracy is 0.00333 seconds. So you need to go to '2009-09-30 23:59:59.998' so it doesn't round up to Oct 1st.
For example:
returns: