SQL LIKE 操作符在它应该显示的时候没有显示任何结果 [英] SQL LIKE operator not showing any result when it should

查看:20
本文介绍了SQL LIKE 操作符在它应该显示的时候没有显示任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含大量信息的 Vehicle 表,但其中一列是 MSSQL 表中的所有者"

I got a Vehicle table with lots of info but one of the columns being 'Owner' in a MSSQL table

但是当我使用 LIKE 时我无法选择所有者之一但是如果我使用 =

But one of the owners i can't select when i use LIKE but can if i use =

'Silkeborg Distributionscenter' 是所有者(是的,表和参数中都有双空格)

'Silkeborg Distributionscenter' is the owner (Yes there is a double space in both the table and the param)

所以参数是:

DECLARE @Owners nvarchar(MAX) = 'Silkeborg  Distributionscenter'

我试过了:

SELECT * FROM Vehicle WHERE @Owners = Owner 
--This gave me all the correct results

那么:

SELECT * FROM Vehicle WHERE @Owners LIKE Owner
--No result

那么:

SELECT * FROM Vehicle WHERE @Owners COLLATE Danish_Norwegian_CI_AS LIKE Owner COLLATE Danish_Norwegian_CI_AS
--No result

我需要使用 LIKE,因为我需要能够一次选择一组所有者.所以原来的select语句是这样的

I need to use LIKE because i need to be able to select a array of owners at once. So the original select statement look like this

SELECT * FROM Vehicle WHERE ('|' + @Owners + '|' like '%|' + Owner + '|%')
--No result

有什么想法吗?

一切正常,@Owners 是Fredericia Distributionscenter"而不是Silkeborg Distributionscenter",它们都在表中.

It all works fine the @Owners is 'Fredericia Distributionscenter' but not 'Silkeborg Distributionscenter', Both of them is within the table.

Silkeborg Distributionscenter"文本中的某些内容是否有效?

Is the something work in the 'Silkeborg Distributionscenter' text?

推荐答案

根据要求升级为答案.

在 Vehicle 表中 Owner 字段的末尾有一些空格字符.'=' 运算符跳过尾随空格,'like' 运算符不跳过.

You have some space characters in the end of your Owner field in Vehicle table. '=' operator skips trailing spaces, 'like' operator doesn't.

这篇关于SQL LIKE 操作符在它应该显示的时候没有显示任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆