70

I'm using an Oracle database and I want to know how can I find rows in a varchar type column where the values of that column has a string which contains some character.

I'm trying something like this (that's a simple example of what I want), but it doesn't work:

select p.name
from   person p
where  p.name contains the character 'A';

I also want to know if I can use a function like chr(1234) where 1234 is an ASCII code instead of the 'A' character in my example query, because in my case I want to search in my database values where the name of a person contains the character with 8211 as ASCII code.

With the query select CHR(8211) from dual; I get the special character that I want.

Example:

select p.name
from   person p
where  p.name contains the character chr(8211);

4 Answers 4

123

By lines I assume you mean rows in the table person. What you're looking for is:

select p.name
from   person p
where  p.name LIKE '%A%'; --contains the character 'A'

The above is case sensitive. For a case insensitive search, you can do:

select p.name
from   person p
where  UPPER(p.name) LIKE '%A%'; --contains the character 'A' or 'a'

For the special character, you can do:

select p.name
from   person p
where  p.name LIKE '%'||chr(8211)||'%'; --contains the character chr(8211)

The LIKE operator matches a pattern. The syntax of this command is described in detail in the Oracle documentation. You will mostly use the % sign as it means match zero or more characters.

2
  • Thank you for the answer, it works. And like you said, by lines I meant rows of course, sorry for the misunderstanding
    – ibaneight
    Dec 3, 2013 at 9:12
  • Glad to help. Yeah, it's easy to go by tables, rows, columns, and cells - like a spreadsheet.
    – ADTC
    Dec 3, 2013 at 9:22
42

The answer of ADTC works fine, but I've find another solution, so I post it here if someone wants something different.

I think ADTC's solution is better, but mine's also works.

Here is the other solution I found

select p.name
from   person p
where  instr(p.name,chr(8211)) > 0; --contains the character chr(8211) 
                                    --at least 1 time
4
  • Use the code formatting instead of bold italic. I'd edit it myself but I'd like you to learn =)
    – ADTC
    Dec 3, 2013 at 9:23
  • Thanks, I hope is better now :D
    – ibaneight
    Dec 3, 2013 at 9:33
  • 1
    This method is useful when you search for the _ and % characters in a string. Searching for them with LIKE is possible but way more cumbersome. Feb 13, 2019 at 15:18
  • 1
    I think this is the best answer, because if a subquery is used in place of chr(8211), it still works. When you use like with a subquery and the subquery return 0 results, the like statement would be like '%%', and it will always match, which is probably not intended.
    – fall
    Sep 8, 2022 at 12:23
8

You used the keyword CONTAINS in your sample queries and question. CONTAINS lets you search against columns that have been indexed with an Oracle*Text full-text index.

Because these columns are full-text indexed, you can efficiently query them to search for words and phrases anywhere with the text columns without triggering a full table scan. Depending upon their usage, using LIKE or INSTR will almost always result in a full table scan.

CONTAINS is used to search for words and phrases. Although there are many options it is not appropriate if you are looking for embedded characters such as 'A' or chr(8211).

The following query will return all rows that contain the word "smith" anywhere in their text.

SELECT score(1), p.name
FROM person p
WHERE CONTAINS(p.name, 'smith', 1) > 0;

For more details see:

2

Just in case you need to find if a column has any values that have character in it, you can use regexp_like. The first parameter is the column name to be checked and the second parameter is the regular expression.

If the below sql returns count greater than zero, that means there are some row(s) in which there is 1 or more character in it.

SELECT COUNT(*) 
FROM TABLE_NAME 
WHERE regexp_like (COLUMN_NAME, '[^0-9]')

Your Answer

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

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