WHERE 子句因某些字符串文字而失败
我有一个存储错误消息的数据库表,我想对它们运行各种查询。首先,我通过如下方式对它们进行分组来获得总体计数:
select MessageText, COUNT(*) from MessageLog group by MessageText
结果是:
1 Input string was not in a correct format 4
2 Value cannot be null. Parameter name: Int 8
3 Value cannot be null. Parameter name: String 1
现在,如果我尝试通过文本字符串选择消息,其中一些消息不会返回任何结果,即使消息存在。例如,
select * from MessageLog where MessageText = 'Value cannot be null. Parameter name: Int'
不返回任何结果,即使前面的查询显示有 8 个结果。这个无法匹配的字符串是怎么回事?
I have a database table that stores error messages, and I want to run various queries on them. First, I get an overall count by grouping them like this:
select MessageText, COUNT(*) from MessageLog group by MessageText
And the result is:
1 Input string was not in a correct format 4
2 Value cannot be null. Parameter name: Int 8
3 Value cannot be null. Parameter name: String 1
Now, if I try to select messages by the text string, some of them return no results even though the messages exist. For example,
select * from MessageLog where MessageText = 'Value cannot be null. Parameter name: Int'
does not return any results, even though the previous query shows there are 8 of them. What is it about this string that fails to match?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的猜测是,您的 MessageText 中要么有您没有考虑到的空白,要么您正在做一些没有向我们展示的事情。我运行了以下查询:
并得到了预期的结果。
My guess is that you either have white spaces in your MessageText that you are not accounting for or you are doing something you are not showing us. I ran the following query:
and got the expected results.
实际消息可能包含您在第二个查询中没有考虑到的空格。
It possible that the actual message contains white space that you are not accounting for in your second query.
也许字符串末尾有空格。为了验证您可以测试:
或者:
Maybe you have white spaces at the end of string. For verify you can test:
Or:
我最好的猜测是空格问题,要么是尾随空格,要么是文本字符串内的额外空格。在比较字段之前尝试对字段进行修剪。
My best guess is a white space issue, either trailing space or extra spaces inside the text string. Try using a trim on the fields before comparing them.
尝试将该查询的输出重定向到文本文件,然后通过 od -c 查看它。里面可能隐藏着一个角色。
Try redirecting the output of that query to a text file, and then looking at it through
od -c
. There could be a hidden character in there.