30

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.

3
  • I hope you are using parameterized queries, as your code may very well be open to SQL Injection. See boby tables
    – Oded
    Oct 13, 2010 at 13:54
  • Where does the parameter come from? (@Oded: I guess that's what '<parameter>' was supposed to mean…)
    – Tomalak
    Oct 13, 2010 at 13:56
  • @Tomalak - I hope you are right...
    – Oded
    Oct 13, 2010 at 13:58

10 Answers 10

68

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>'
2
  • What about the case when an index is defined on [table].[column]?
    – bjnr
    Feb 14, 2014 at 6:59
  • 1
    On oracle (at least the version I'm using, 10.2), coalesce doesn't work with an empty string. I have to use COALESCE([table].[column],' ') to match fields. Sep 11, 2019 at 13:23
8
isnull([table].[column], '') like '%'

Works like a charm

3

I think this might work:

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

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.

2

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..

1

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

Select * From [table] Where [table].[column] like '%' or [table].[column] is null;
3
  • Surprisingly, NULL != NULL , you need to use the special operator IS NULL Oct 13, 2010 at 13:58
  • Isn't that the same as select * from [table]?
    – Caramiriel
    Oct 6, 2015 at 14:20
  • Yes, but usually you'll have more where fields, and the % could be a parameter supplied by PHP, for example
    – Berry M.
    Oct 2, 2016 at 10:29
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
1
  • 1
    This answer is just a lump of code, it seems very similar to another answer. Please explain why this is a good answer, how the code works and how it differs from the other answers.
    – AdrianHHH
    Oct 6, 2015 at 12:37
0

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

0

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.

1
  • coalesce works on Oracle. Your code is equivalent to where coalesce(table1.field, ' ') like '%'
    – trincot
    Jul 15, 2016 at 20:01
0

For SQLITE

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.