_ (underline) in consultation with LIKE

Asked

Viewed 3,392 times

9

I’m making a query where I need to return all information from a given table, which contains "_R_" in the nomenclature.

However, when using WHERE NM_CAMPAIGN LIKE '%_R_%' he returns me to other cases that have only "R_".

How else can I use that criterion?

  • 1

    Which one SGBD?

  • That’s why the character _ is also a SQL wildcard character. If you need it literal, you need to escape it.

  • I use SQL Server Management Studio.

  • 1

    Reinforcing for future readers:(Wildcard - matches a character) (Transact-SQL)

  • 1

    @Marconi put this reference in the answer

3 answers

15


Your case SGBD be the SQL Server you need to escape the character:

SELECT t.*
  FROM tabela t
 WHERE t.NM_CAMPAIGN LIKE '%[_]R[_]%'

LIKE

Determines whether a specific character string matches a specified pattern. A pattern can include normal and wildcard characters. During pattern matching, normal characters should match exactly the characters specified in the character string. However, wildcards can be matched to arbitrary fragments of the string. The use of wildcards makes the operator LIKE more flexible than the use of string comparison operators = and !=. If any of the arguments are not of the character string data type, the SQL Server will convert it to the character string data type, if possible.


_ (wildcard - corresponds to a character)

Use the underlined character _ to match any single character in a string comparison operation that involves matching patterns, such as LIKE and PATINDEX.


In the case of LIKE when you use the character _ is specifying the occurrence of any character, ie for the instruction %_R_% you are specifying that you want any result that:

  • Possess 0 or more characters at the beginning;
  • is followed by an occurrence of any character;
  • Have the letter R;
  • Have any character followed by anything at the end;

In your case I will list some possible results:

- 0R0;
- 000R0;
- 000R000;
- 0_R_0;

Results that would not be filtered by your search:

- R0;
- 0R;
- R000;
- 000R;
- A;
- R;

That’s why the results with _R_ were filtered, since they obey the rule, but are not the only ones that coincide with what was specified.

  • This works that way on Oracle and Mysql as well ;)

  • @Renan thanks for the feedback. I will include in the answer!

  • For a moment I thought I saw that you were escaping the backslash character. The bar works on all DBMS’s. The clasps I’m not sure =\

  • @Renan hehehe quiet, I will change the answer again

  • 1

    @Sorack got it! Thank you

  • The person responsible for downvote could say which problem should be corrected in this reply justifying this vote?

Show 1 more comment

11

The underline has a meaning similar to the percentage sign in condition LIKE SQL. Both are wildcards.

The difference is as follows:

  • % looking for zero or more occurrences of any character;
  • _ looking for one or more occurrences of any character.

For example, suppose we have a table with a column called word, and that the table has the following registered words:

class, class, class, class, class

Then the results of the queries will be the following

... where PALAVRA like '%cat%'
-- retorna acata, categoria, catraca, escatologia, mercator

... where PALAVRA like '_cat_'
-- retorna acata

-- Combinando os dois agora:
... where PALAVRA like '%_cat_%'
-- retorna acata, escatologia, mercator

To search for the wildcard characters themselves, you must escape them with a backslash. Your Where clause should look like this:

... where NM_CAMPAIGN like '%\_R\_%'

This goes for the three main DBMS: SQL Server, Oracle and Mysql.

  • 3

    That, perfect. Giving the solution is legal, explaining why the problem is so much better.

  • 1

    I’ve already left my +1, but I can’t understand the difference between % and _in practice. You can add an example @Renan?

  • @Marconi a minute I put

  • @Renan, the character _ looking for only one character, no?

  • @Marconi the _ in reality requires a lot of characters, so in reality when you put _R you say you want everything that has 2 characters and ends in R. In case it only requires that it has 1 or more characters

  • @Sorack doesn’t want to include a Fiddle in his reply? I’ve already left my +1 there as well. I think an example makes understanding easier.

  • 1

    @Marconi I’ll include some examples, it may be?

  • @Sorack Of course I do :)

  • @Marconi I didn’t understand, and Fiddle just confirms what I said in the post.

  • @Marconi I suggest to check in the fiddle that "_x_" and %_x_% are equivalent. If you still notice something that I haven’t seen here, you can put your own answer (with Fiddle). I honestly haven’t figured out the difference yet.

  • 1

    @Marconi ah, now it’s on. Thanks!

Show 6 more comments

2

I’ve been there, I’ve come up with my solution of that answer.

SELECT * FROM mytable
WHERE LOWER(REPLACE(post_code_field, ' ', '')) = LOWER(REPLACE(?, ' ', ''))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.