Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use string contains function in oracle SQL query

Tags:

sql

oracle

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); 
like image 382
ibaneight Avatar asked Dec 03 '13 08:12

ibaneight


People also ask

How use contains in Oracle SQL query?

The CONTAINS operator must always be followed by the > 0 syntax, which specifies that the score value returned by the CONTAINS operator must be greater than zero for the row to be returned.

How do you check if a string contains a word in Oracle SQL?

The Oracle INSTR function is used to search string for substring and find the location of the substring in the string. If a substring that is equal to substring is found, then the function returns an integer indicating the position of the first character of this substring.

How do you check if a string contains a value in SQL?

Method 1 - Using CHARINDEX() function This function is used to search for a specific word or a substring in an overall string and returns its starting position of match. In case no word is found, then it will return 0 (zero).

How do I use contains in SQL?

CONTAINS is a predicate used in the WHERE clause of a Transact-SQL SELECT statement to perform SQL Server full-text search on full-text indexed columns containing character-based data types. CONTAINS can search for: A word or phrase. The prefix of a word or phrase.


1 Answers

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.

like image 114
ADTC Avatar answered Sep 23 '22 02:09

ADTC