简体   繁体   English

为什么MS SQL Server使用'LIKE'查询返回结果,但使用'='返回空集

[英]Why does MS SQL Server return results with 'LIKE' query but returns empty set using '='

I'm using utilizing MS SQL Server 2017. In this example, I have a table [myTable] with one entity full name (varchar(255) . 我正在使用MS SQL Server 2017.在这个例子中,我有一个表[myTable],其中有一个实体full name (varchar(255)

Inside of this table, I have one record bulk imported with the following parameters 在这个表的内部,我有一个使用以下参数导入的记录批量

(FIELDTERMINATOR = '~',
 ROWTERMINATOR = '0x0A' )

The following query returns the single record: 以下查询返回单个记录:

SELECT [full name]
FROM myTable
WHERE [full name] LIKE '%LastName%'

However, the following query does not: 但是,以下查询不会:

SELECT [full name]
FROM myTable
WHERE [full name] = 'Firstname Lastname'

I have verified the record exists, I have copied the value of the entity to make sure there was no funny business going on with the data. 我已经验证了记录存在,我已经复制了实体的价值,以确保没有有趣的业务继续进行数据。 It comes up clean, I even tried this: 它干净,我甚至试过这个:

SELECT [full name]
FROM myTable
WHERE [full name] = 'Firstname Lastname ' 

to make sure there wasn't a random space at the end of the column. 确保列末尾没有随机空间。 What would cause an issue like this? 什么会导致像这样的问题?

The = operator works only when it got exact match =运算符仅在完全匹配时才起作用

but The LIKE operator is used in a to search for a specified pattern in of a data. 但是LIKE运算符用于搜索数据中的指定模式。

so in your case your compare value is not directly matched with the column values that's why it does not returned any rows but in times like it matched pattern so it returned rows 因此,在您的情况下,您的比较值不会与列值直接匹配,这就是为什么它不返回任何行,但在匹配模式的时候,所以它返回行

LIKE as the name itself suggests, is mainly used to compare data of some form that form is mostly regular expressions LIKE作为本身暗示的名称,主要用于比较的某种形式的数据形式,主要是regular expressions

whereas, 然而,

the = finds only the exact match not that it should be of some form rather the data on left of = should be a crystal clear copy of whats on the right of it. =只找到完全匹配而不是它应该是某种形式而不是=左边的数据应该是它右边的一个清晰的副本。

Eg. 例如。

As per your above input sample ..where fullname LIKE %FirstName% . 根据您的上述输入示例..where fullname LIKE %FirstName% This would mean in general, select data Where fullname has firstname in it 这通常意味着选择数据其中fullname中包含firstname

If I replace this % ...where fullname LIKE FirstName then it would be same as ...where fullname = FirstName coz here it wants exact match not data of some form 如果我替换这个% ...where fullname LIKE FirstName那么它将与...where fullname = FirstName coz在这里它想要完全匹配而不是某种形式的数据

I have a handy script I use to identify invisible characters; 我有一个方便的脚本用于识别不可见的字符; it gives you every character in the string but as a separate row per character. 它为字符串中的每个字符提供但每个字符作为一个单独的行。

DECLARE @nstring NVARCHAR(1000)

select top 1 @nstring = [full name]
FROM myTable
WHERE [full name] LIKE '%LastName%'

print @nstring

DECLARE @position INT
 SET @position = 1

DECLARE @CharList TABLE (
 Position INT,
 UnicodeChar NVARCHAR(1),
 UnicodeValue INT
 )

WHILE @position <= DATALENGTH(@nstring)
 BEGIN
 INSERT @CharList
 SELECT @position as Position
 ,CONVERT(nchar(1),SUBSTRING(@nstring, @position, 1)) as UnicodeChar
 ,UNICODE(SUBSTRING(@nstring, @position, 1)) as UnicodeValue
 SET @position = @position + 1
 END

SELECT * FROM @CharList

There is a tip given on mssqltips that says you can use SSIS. mssqltips上有一个提示,说明你可以使用SSIS。 The article also explains this issue occurring with the ROWTERMINATOR. 本文还解释了ROWTERMINATOR发生的这个问题。 With that said, I would create an SSIS project that contains a package that does the bulk data insert from the data source. 话虽如此,我将创建一个SSIS项目,其中包含一个从数据源执行批量数据插入的包。 If this is a repeated process it can be scheduled or quickly tested in the SSMS. 如果这是一个重复的过程,可以在SSMS中安排或快速测试。

If you are new to SSIS or are not familiar with SSIS, refere here: SQL Server Integration Services 如果您是SSIS的新手或不熟悉SSIS,请参阅此处: SQL Server Integration Services

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM