SQL 像 '%' 不接受 NULL 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3924400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 07:54:56  来源:igfitidea点击:

like '%' does not accept NULL value

sqlnull

提问by zgorawski

I have query that is build from user's inputs ( passed via html form). It looks like (simple example):

我有根据用户输入构建的查询(通过 html 表单传递)。它看起来像(简单的例子):

Select * From [table] Where [table].[column] like '<parameter>'

This parameter may be optional so if user left corresponding input field empty I pass %. It worked fine until I encountered NULL values. I understand that '%' match symbols not null, but i would like to consider NULL as empty string in this case.

此参数可能是可选的,因此如果用户将相应的输入字段留空,我会传递%。它工作正常,直到我遇到 NULL 值。我知道 '%' 匹配符号不为空,但我想在这种情况下将 NULL 视为空字符串。

What should I do? Change query (how?) or pass another symbol(s) when user left empty input?

我该怎么办?当用户留空输入时更改查询(如何?)或传递另一个符号?

Thanks.

谢谢。

PS.It's real problem from existing system and i know it's far from optimal solution, but i have to deal with it.

附注。这是现有系统的真正问题,我知道它远非最佳解决方案,但我必须处理它。

回答by Andomar

You can use coalesceto treat null like an empty string:

您可以使用coalesce将 null 视为空字符串:

where COALESCE([table].[column],'') like '<parameter>'

On SQL Server, you can also use IsNull:

在 SQL Server 上,您还可以使用IsNull

where IsNull([table].[column],'') like '<parameter>'

回答by Pedro Rodrigues

isnull([table].[column], '') like '%'

Works like a charm

奇迹般有效

回答by bernhardrusch

I think this might work:

我认为这可能有效:

Select * From [table] Where [table].[column] is null or [table].[column] like '<parameter>'

回答by Matt Gibson

Well, how about

嗯,怎么样

SELECT 
  * 
FROM 
  [table] 
WHERE
  ([table].[column] like <parameter>) OR 
  (<parameter> = '%')

...so that when you pass '%', you get all the rows back, otherwise it works like you have it at the moment.

...这样,当您传递 '%' 时,您会返回所有行,否则它就像您现在拥有的那样工作。

回答by Tim

Make two statements! If the user passed no parameter user:

发表两个声明!如果用户没有传递参数 user:

Select * From [table] Where [table].[column] like '%' or [table].[column] is null;

回答by Shawson

how about..

怎么样..

Select * From [table] Where ISNULL([table].[column], '') like '<parameter>'

So this will take your actual column value, or if thats null an empty string and compare it aganist your parameter, assuming your using ms sql server..

因此,这将采用您的实际列值,或者如果那是空字符串,则将其与您的参数进行比较,假设您使用的是 ms sql server..

回答by HaMEd

Based On Index in Where ClauseIssue With This Approach

基于此方法的Where 子句问题中的索引

where COALESCE([table].[column],'') like '<parameter>'

Is :

是 :

If you have used an Index on your [column], because of the COALESCE function,SQL Server cant use your index,it means that you've wasted your index

如果你在你的 [column] 上使用了索引,因为 COALESCE 函数,SQL Server 不能使用你的索引,这意味着你浪费了你的索引

AND

Issue With This Approach

这种方法的问题

Where [table].[column] like '%' or [table].[column] is null

Is :

是 :

If the [table].[column] is null then the code will be like this :

如果 [table].[column] 为 null,则代码将如下所示:

Where null like '%' or [table].[column] is null

and regardless of second part of Where clause ([table].[column] is null) the result of evaluation will be UNKNOWN and the SQL Server filter that record out.

并且无论 Where 子句的第二部分([table].[column] 为空),评估结果都将是 UNKNOWN 并且 SQL Server 过滤器记录出来。

NULL OR True = UNKNOWN

NULL 或 True = 未知

NULL OR False = UNKNOWN

NULL 或 False = 未知

So this is the optimized and null included approach :

所以这是优化和包含空值的方法:

Select * From [table] 
 WHERE 
      CASE 
          WHEN [table].[column] IS NULL THEN 1 
          WHEN [table].[column] like '<parameter>' THEN 1 
          ELSE 0 
      END   =  1

回答by Neftali Rios

Good day, use this solution, I think it's a mixture of solutions:

美好的一天,使用此解决方案,我认为这是解决方案的混合:

@parameter nvarchar (30)

if @parameter = ''
????? Begin
????????? Set @parameter = '%'
????? End

select * from [table] as t where ISNULL (t. [column], '') like '%' + @parameter + '%'

If you just want to start with the parameter, removes the first '%' and the plus sign

如果只想以参数开头,则删除第一个 '%' 和加号

I hope you find it useful

希望对你有帮助

回答by nathie

I wanted something similar, to actually be able to find '%' (all) including nullor a specific value when input.

我想要类似的东西,实际上能够在输入时找到 '%' (all) 包括null或特定值。

For Oracle isnulldoes not work and in my case COALESCEneither.

对于 Oracleisnull不起作用,在我的情况下COALESCE也不起作用。

I Used this option in the where clause:

我在 where 子句中使用了这个选项:

where decode(table1.field1,null,' ',table1.field1) like '%'

Hope it works for others.

希望它对其他人有用。

回答by ndw

For SQLITE

对于 SQLITE

SELECT * FROM [table] WHERE IFNULL([table].[column],'') like '<parameter>'