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.

zgorawski
  • 2,597
  • 4
  • 30
  • 43
  • I hope you are using parameterized queries, as your code may very well be open to SQL Injection. See [boby tables](http://xkcd.com/327/) – Oded Oct 13 '10 at 13:54
  • Where does the parameter come from? (@Oded: I guess that's what `''` was supposed to mean…) – Tomalak Oct 13 '10 at 13:56
  • @Tomalak - I hope you are right... – Oded Oct 13 '10 at 13:58

10 Answers10

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>'
Andomar
  • 232,371
  • 49
  • 380
  • 404
  • What about the case when an index is defined on [table].[column]? – bjnr Feb 14 '14 at 06: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. – Teleporting Goat Sep 11 '19 at 13:23
8
isnull([table].[column], '') like '%'

Works like a charm

Pedro Rodrigues
  • 2,520
  • 2
  • 27
  • 26
3

I think this might work:

Select * From [table] Where [table].[column] is null or [table].[column] like '<parameter>'
bernhardrusch
  • 11,670
  • 12
  • 48
  • 59
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.

Matt Gibson
  • 37,886
  • 9
  • 99
  • 128
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..

Shawson
  • 1,858
  • 2
  • 24
  • 38
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
Community
  • 1
  • 1
HaMEd
  • 43
  • 1
  • 8
  • 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 06 '15 at 12:37
1

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

Select * From [table] Where [table].[column] like '%' or [table].[column] is null;
Tim
  • 2,831
  • 1
  • 25
  • 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

Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
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.

trincot
  • 317,000
  • 35
  • 244
  • 286
nathie
  • 1
  • `coalesce` works on Oracle. Your code is equivalent to `where coalesce(table1.field, ' ') like '%'` – trincot Jul 15 '16 at 20:01
0

For SQLITE

SELECT * FROM [table] WHERE IFNULL([table].[column],'') like '<parameter>'
ndw
  • 513
  • 6
  • 14