为什么 LIKE 不返回带有 '%' 的变量的行 最后?
我发现这在 Microsoft SQL Server 上很奇怪:
SELECT * FROM deliveries WHERE code LIKE '01999195000%'
-- 9 rows returned. Works.
DECLARE @a VARCHAR(10)
SET @a='01999195000%'
SELECT * FROM deliveries WHERE code LIKE @a
-- 0 rows returned? Why not?
SET @a = '01999195000'
SELECT * FROM deliveries WHERE code LIKE @a + '%'
-- 9 rows returned. Works.
搜索包含 % 字符的 @a 和不包含但附加 '%' 的 @a 之间有什么不同?
如果你们中的任何一位 SQL 专家能够分享您的想法,那就太好了。
I find this quite odd on Microsoft SQL Server:
SELECT * FROM deliveries WHERE code LIKE '01999195000%'
-- 9 rows returned. Works.
DECLARE @a VARCHAR(10)
SET @a='01999195000%'
SELECT * FROM deliveries WHERE code LIKE @a
-- 0 rows returned? Why not?
SET @a = '01999195000'
SELECT * FROM deliveries WHERE code LIKE @a + '%'
-- 9 rows returned. Works.
What is different between searching for @a which includes the % character, and one that does not but has '%' appended?
If any of you SQL Guru's could share your thoughts, that would be great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为您已将 @a 定义为 VARCHAR(10),但您尝试将 12 个字符放入其中...意味着“%”从末尾丢失
It's because you've defined @a as a VARCHAR(10), but you've tried putting 12 characters into it...meaning the "%" gets lost from the end
DECLARE @a VARCHAR(10) 就是答案。 @a 从不包含 %。
DECLARE @a VARCHAR(10) is the answer. @a never contains the %.
LIKE 是一个通配符,意思是“这里你喜欢的任何东西”。
LIKE is a wildcard character, meaning "anything you like here".