WHERE 子句因某些字符串文字而失败

发布于 2024-12-10 10:50:39 字数 564 浏览 0 评论 0原文

我有一个存储错误消息的数据库表,我想对它们运行各种查询。首先,我通过如下方式对它们进行分组来获得总体计数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

天赋异禀 2024-12-17 10:50:39

我的猜测是,您的 MessageText 中要么有您没有考虑到的空白,要么您正在做一些没有向我们展示的事情。我运行了以下查询:

with MessageLog as
(
    select 1 as id, 'Input string was not in a correct format' as MessageText, 4 as count
    UNION 
    select 2 as id, 'Value cannot be null. Parameter name: Int' , 8
    UNION
    select 3 as id, 'Value cannot be null. Parameter name: String' , 1
)
select * from MessageLog where MessageText = 'Value cannot be null. Parameter name: Int'

并得到了预期的结果。

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:

with MessageLog as
(
    select 1 as id, 'Input string was not in a correct format' as MessageText, 4 as count
    UNION 
    select 2 as id, 'Value cannot be null. Parameter name: Int' , 8
    UNION
    select 3 as id, 'Value cannot be null. Parameter name: String' , 1
)
select * from MessageLog where MessageText = 'Value cannot be null. Parameter name: Int'

and got the expected results.

少女情怀诗 2024-12-17 10:50:39

实际消息可能包含您在第二个查询中没有考虑到的空格。

It possible that the actual message contains white space that you are not accounting for in your second query.

内心旳酸楚 2024-12-17 10:50:39

也许字符串末尾有空格。为了验证您可以测试:

select * from MessageLog where MessageText LIKE 'Value cannot be null. Parameter name: Int%'

或者:

select * from MessageLog where TRIM(MessageText) = 'Value cannot be null. Parameter name: Int'

Maybe you have white spaces at the end of string. For verify you can test:

select * from MessageLog where MessageText LIKE 'Value cannot be null. Parameter name: Int%'

Or:

select * from MessageLog where TRIM(MessageText) = 'Value cannot be null. Parameter name: Int'
被翻牌 2024-12-17 10:50:39

我最好的猜测是空格问题,要么是尾随空格,要么是文本字符串内的额外空格。在比较字段之前尝试对字段进行修剪。

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.

往事随风而去 2024-12-17 10:50:39

尝试将该查询的输出重定向到文本文件,然后通过 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文