就像'%'不接受 NULL 值

发布于 2024-09-26 23:13:53 字数 382 浏览 3 评论 0原文

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

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

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

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

谢谢。

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

七颜 2024-10-03 23:13:53

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

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

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

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

You can use coalesce to treat null like an empty string:

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

On SQL Server, you can also use IsNull:

where IsNull([table].[column],'') like '<parameter>'
娇柔作态 2024-10-03 23:13:53
isnull([table].[column], '') like '%'

就像魅力一样工作

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

Works like a charm

微凉徒眸意 2024-10-03 23:13:53

我认为这可能有效:

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

I think this might work:

Select * From [table] Where [table].[column] is null or [table].[column] like '<parameter>'
南巷近海 2024-10-03 23:13:53

好吧,怎么样

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

...这样当您传递 '%' 时,您将获得所有行,否则它就像您现在拥有它一样。

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.

北座城市 2024-10-03 23:13:53

怎么样..

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

所以这将采用您的实际列值,或者如果它为空,则为空字符串并将其与您的参数进行比较,假设您使用的是 MS SQL 服务器。

How about..

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

So this will take your actual column value, or if that's null an empty string and compare it against your parameter, assuming you're using MS SQL server..

夜深人未静 2024-10-03 23:13:53

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

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

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

Select * From [table] Where [table].[column] like '%' or [table].[column] is null;
旧伤还要旧人安 2024-10-03 23:13:53

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

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

是:

如果您在[列]上使用了索引,由于COALESCE功能,SQL Server无法使用您的索引,这意味着您已经浪费了索引

并且

这种方法的问题

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

是:

如果[表] .[column] 为 null 那么代码将如下所示:

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

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

NULL OR True = UNKNOWN

NULL OR False = UNKNOWN

因此,这是优化且包含 null 的方法:

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

Based On Index in Where Clause Issue With This Approach

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

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 :

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.

NULL OR True = UNKNOWN

NULL OR False = UNKNOWN

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
笑饮青盏花 2024-10-03 23:13:53

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

@parameter nvarchar (30)

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

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

如果您只想从参数开始,请删除第一个“%”和加号

我希望您发现它有用

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

小巷里的女流氓 2024-10-03 23:13:53

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

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

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

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

希望它对其他人有用。

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

For Oracle isnull does not work and in my case COALESCE neither.

I Used this option in the where clause:

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

Hope it works for others.

各自安好 2024-10-03 23:13:53

对于 SQLITE

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

For SQLITE

SELECT * FROM [table] WHERE IFNULL([table].[column],'') like '<parameter>'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文