how to skip particular character(s) in SQL LIKE query - sql

I have a table(say users) in which there is a column say name.
you may think table structure a shown below:
-------------
name
--------------
Abdul Khalid
--------------
Abdul, Khalid
--------------
Abdul - Khalid
--------------
other names
My question is can I do some query to find all the 3 rows in which the name column value is "Abdul Khalid"(basically "Abdul Khalid" or "Abdul, Khalid" or "Abdul - Khalid" if I skip the "," and "-" character).

You can use like:
select t.*
from t
where name like 'Abdul%Khalid';
If you want the names anywhere in the string (but in that order), then put wildcards at the beginning:
select t.*
from t
where name like '%Abdul%Khalid%';
If you are passing in the value as a variable:
select t.*
from t
where name like replace('Abdul Khalid', ' ', '%');

For PostgreSQL is better to use '~'
name ~ '^Abdul[ ,-]Khalid$'
OR if you want also in middle of string:
name ~ 'Abdul[ ,-]Khalid'
Or you can use translate (with index on it) for any SQL:
translate(name, ' ,-') = 'AbdulKhalid'

you also can use REGEXP like this:
SELECT * from yourTable where name REGEXP 'Abdul( |, | - )Khalid';

Related

Remove single quotes in Oracle

I have a string like (''acc','xyz''), I need the output as ('acc','xyz').
What will be the query or any regular expression to remove extra quotes.?
Try REPLACE function:
replace(q'[(''acc','xyz'')]', q'['']',q'[']')
Demo: http://www.sqlfiddle.com/#!4/abb5d3/1
SELECT replace(q'[(''acc','xyz'')]', q'['']',q'[']')
FROM dual;
| REPLACE(Q'[(''ACC','XYZ'')]',Q'['']',Q'[']') |
|----------------------------------------------|
| ('acc','xyz') |
If that string always looks like you described, then replace two consecutive single quotes (CHR(39)) with a single one, such as
SQL> with test (col) as
2 (select q'[(''acc','xyz'')]' from dual)
3 select col,
4 replace(col, chr(39)||chr(39), chr(39)) result
5 from test;
COL RESULT
--------------- ---------------
(''acc','xyz'') ('acc','xyz')
SQL>
Why CHR(39)? Because this: replace(col, '''''', '''') is difficult to read, and this: replace(col, q'['']', q'[']') looks stupid, but - use any of these (or invent your own way).

Select statement with column contains '%'

I want to select names from a table where the 'name' column contains '%' anywhere in the value. For example, I want to retrieve the name 'Approval for 20 % discount for parts'.
SELECT NAME FROM TABLE WHERE NAME ... ?
You can use like with escape. The default is a backslash in some databases (but not in Oracle), so:
select name
from table
where name like '%\%%' ESCAPE '\'
This is standard, and works in most databases. The Oracle documentation is here.
Of course, you could also use instr():
where instr(name, '%') > 0
One way to do it is using replace with an empty string and checking to see if the difference in length of the original string and modified string is > 0.
select name
from table
where length(name) - length(replace(name,'%','')) > 0
Make life easy on yourselves and just use REGEXP_LIKE( )!
SQL> with tbl(name) as (
select 'ABC' from dual
union
select 'E%FS' from dual
)
select name
from tbl
where regexp_like(name, '%');
NAME
----
E%FS
SQL>
I read the documentation mentioned by Gordon. The relevent sentence is:
An underscore (_) in the pattern matches exactly one character (as opposed to one byte in a multibyte character set) in the value
Here was my test:
select c
from (
select 'a%be' c
from dual) d
where c like '_%'
The value a%be was returned.
While the suggestions of using instr() or length in the other two answers will lead to the correct answer, they will do so slowly. Filtering on function results simply take longer than filtering on fields.

Select only when the field has more than one word

SELECT name FROM clients;
Table clients
id | name |
1 John
2 John Bravo
3 John Alves
4 Jo
In postgres, how can I select only names with more than one word? For example. This should be the output:
John Bravo
John ALves
I think just test if the name contains a space: where name like '% %'
But this will give you some problem if you name can contain space char befor or after the name, like: ' JOHN' or 'Luck '
If word means to you a space delimited token, then the following would do the trick:
SELECT name FROM clients WHERE name LIKE '% %';
But this will also give you those that have empty names made out of spaces only. Also performance-wise, this will be a costly query.
First you may have to find the number of words in the column, then only select where the number of words is greater than 1.
For example:
SELECT LENGTH(name) - LENGTH(REPLACE(name , ' ', ''))+1 FROM clients;
This will return the number of words in each row, which we have in the field name.
Then you can proceed putting this in a nested select SQL statement something like :
SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1 as mycheck, name FROM clients where (SELECT LENGTH(name) - LENGTH(REPLACE(name, ' ', ''))+1)>1
This will return only where words are greater than 1 (>1). Else you can user >2 to return columns with more than 2 words.
LIKE is an option, or you can use split_part:
SELECT name FROM clients WHERE split_part(trim(name), ' ', 2) <> ''

SQL select query with wildcard on input parameter

Same question as my other thread basically!
I have a db table and I need to write a query that will return results based on a partial match of a string.
Example:
DB field: abc
Search term: 123abc
i.e. I want given 123abc for it to return the row that has the abc field in it!
My attempt:
SELECT mood from users where '$searchTerm' like '%' || dbField
Is something like that possible in any way?
Well basically I'm trying to match the numbers with the search term la77740985
id | mood | numberfield
==== ===== ============
1 bad '77740985'
2 good '77513755'
Running the query returns both rows!
Note: The wildcard should only be in the beginning of the string in other words I want the search term to begin with anything but still match the string from the database that basically have the same ending.
It worked like this:
SELECT mood from users where '$searchTerm' like concat('%',numberField);
SELECT mood
from users
where '$searchTerm' like '%' || numberField
This will match $searchTerm = '123abc' against dbField that contains 'abc'. It's correct. If you need contains (anywhere), then add || '%' at the end.
Full test script
drop table if exists users2;
create table users2 (mood, numberfield);
insert into users2(mood,numberfield) values ('happy','77740985');
insert into users2(mood,numberfield) values ('sad','77513755');
Then run this
SELECT mood from users2 where 'la77740985' like ('%' || numberfield);
Output
mood
=======
'happy'

SELECT NAME FROM ABCD WHERE NAME LIKE "+aero+%"AND ROWNUM <= 10

SELECT NAME FROM ABCD WHERE NAME LIKE "+aero+%"AND ROWNUM <= 10
what is the syntax error in this line......SELECT NAME FROM ABCD this is working
You need single quotes:
SELECT NAME FROM ABCD WHERE NAME LIKE '+aero+%' AND ROWNUM <= 10
And also a space before AND.
UPDATE
It's not clear in your question what exactly you're searching for. You may need one of the following instead:
SELECT NAME FROM ABCD WHERE NAME LIKE '"+aero+%"' AND ROWNUM <= 10
SELECT NAME FROM ABCD WHERE NAME LIKE '%"+aero+"%' AND ROWNUM <= 10
SELECT NAME FROM ABCD WHERE NAME LIKE '"%+aero+%"' AND ROWNUM <= 10
... or some other variation. But the important thing is that you should surrond literals with single quotes and not double quotes.
EDIT:
con.prepareStatement(
"SELECT name FROM abcd WHERE name LIKE '" + aero + "%' AND ROWNUM <= 10");
Will find all strings starting with your variable aero.
To get all strings containing the String of your variable aero use
con.prepareStatement(
"SELECT name FROM abcd WHERE name LIKE '%" + aero + "%' AND ROWNUM <= 10");
You need to use single-quotes instead of double-quotes:
SELECT name
FROM abcd
WHERE name LIKE '+aero+%'
AND ROWNUM <= 10;
If the double-quotes are in the string you search use '"+aero+%"'.
If you only want to search for strings containing aero, use '%aero%'.
Double quotes around the literal?
No space between the literal and AND?
You need to give the character between the expression and AND operator . And give single quotes for
your regular expression
If you called that from some program codes, following is more likely, where aero is the variable, and % would be acts as wildcard. When aero is ab, it will search for 'ab','abc', 'abcd', .......
"SELECT NAME FROM ABCD WHERE NAME LIKE '"+aero+"%' AND ROWNUM <= 10;"
It isn't clear to me whether your aero is a literal string 'aero' or the name of a variable in your program. If it is a literal then do this:
pr = con.prepareStatement
("SELECT NAME FROM ABCD WHERE NAME LIKE 'aero%' AND ROWNUM <= 10");
If it is a variable then you should use a bind variable. Not only is the syntax much less confusing, it also prevents SQL injection attacks. I'm no ASP expert, but the syntax is something like:
pr = con.prepareStatement
("SELECT NAME FROM ABCD WHERE NAME LIKE ?||'%' AND ROWNUM <= 10");
con.setString(1,aero);