operation LIKE in PL/SQL with string that contain "_" symbol - sql

I have to do a simple select query with the LIKE operator but the problem is that the string to be compared with the LIKE operator contains the symbol "_", for example:
TRY_1245
POSTM_A422
PREP_1000X
And in the table on the DB i have:
TRY_
POSTM_
PREP_
How can I make the query work like this? because if I do the simple LIKE "TRY_1235"
the query returns nothing.
Thanks a lot in advance everyone!

I think you want:
WHERE 'TRY_1235' LIKE col || '%'
The column can be the pattern.
Note that this will match 'TRYST' because _ is a wildcard character. Perhaps the simplest solution is REGEXP_LIKE() -- assuming there are no other unusual characters:
WHERE REGEXP_LIKE('TRY_12345', '^' || col)
Or just escape the _:
WHERE 'TRY_12345' LIKE REPLACE(col, '_', '\_') || '%'

Related

SQL Using LIKE and ANY at the same time

I have a table with a column feature of type text and a text array (text[]) named args. I need to select from the table those rows in which the feature column contains at least one of the elements of the args array.
I've tried different options, including this:
SELECT * FROM myTable WHERE feature LIKE '%' + ANY (args) + '%';
But that does not work.
The simple solution is to use the regular expression match operator ~ instead, which works with strings in arg as is (without concatenating wildcards):
SELECT *
FROM tbl
WHERE feature ~ ANY(args);
string ~ 'pattern' is mostly equivalent to string LIKE '%pattern%', but not exactly, as LIKE uses different (and fewer) special characters than ~. See:
Escape function for regular expression or LIKE patterns
If that subtle difference is not acceptable, here is an exact implementation of what you are asking for:
SELECT *
FROM tbl t
WHERE t.feature LIKE ANY (SELECT '%' || a || '%' FROM unnest(t.args) a);
Unnest the array, pad each element with wildcards, and use LIKE ANY with the resulting set.
See:
IN vs ANY operator in PostgreSQL

Match Character Whether or Not It Exists in Like Statement

I need a like expression that will match a character whether or not it exists. It needs to match the following values:
..."value": "123456"...
..."value": "123456"...
"...value":"123456"...
This like statement will almost work: LIKE '%value":%"123456"%'
But there are values like this one that would also match, but I don't want returned:
..."value":"99999", "other":"123456"...
A regex expression to do what I'm looking to do is 'value": *?"123456"'. I need to do this in SQL Server 2008 and I don't believe there is good regex support in that version. How can I match using a like statement?
Remove the whitespace in your compare with REPLACE():
WHERE REPLACE(column,' ','') LIKE '%"value":"123456"%'
May need a double replace for tabs:
REPLACE(REPLACE(column,' ',''),' ','')
I don't think you can with the like operator. You could exclude ones you could match, like if you want to make sure it just doesn't contain other:
[field] LIKE '%value":%"123456"%` AND [field] NOT LIKE '%"other"%'
Otherwise I think you'd have to do some processing on the string. You could write a UDF to take the string and parse it to find the value for 'value' and compare based on that:
dbo.fn_GetValue([field], 'value') = '123456'
The function could find the index of '"' + #name + '"', find the next index of a quote, and the one after that, then get the string between those two quotes and return it.

Any function escaping a string for literal LIKE search?

I noticed that performance for a filter in Oracle can be very different depending on whether you write
WHERE col LIKE 'abc%'
or
WHERE SUBSTR(col,1,3) = 'abc'
So, I would like to always use LIKE queries. But my prefix can contain any ASCII character, and thus I have to escape %, _, and the escape character itself.
I could use a regexp_replace for anything like that:
WHERE col LIKE REGEXP_REPLACE(pat, '([%_\\])', '\\\1') || '%' ESCAPE '\'
I could also do a triple REPLACE:
WHERE col LIKE REPLACE(REPLACE(REPLACE(pat,'\','\\'),'%','\%'),'_','\_') || '%' ESCAPE '\'
Both ways are painful for the guy that will read it afterwards. Is there an alternative function, in the style of WHERE begins_with(col, pat) > 0, or WHERE col LIKE like_literal(pat) || '%' or do I have to write it myself?
You can determine your own escape character.
WHERE col LIKE 'A\_B%' ESCAPE '\';
In the pattern, the escape character precedes the underscore (_). This causes Oracle to interpret the underscore literally, rather than as a special pattern matching character.
Please read following link
https://docs.oracle.com/cd/B12037_01/server.101/b10759/conditions016.htm

Remove " " from values

In PostgreSQL a column contains values such as:
"Sample1"
"Sample2"
Is there an efficient way to go through each record removing the " " marks?
This seems like a relatively straightforward operation, but I am baffled.
Try this:
UPDATE TableName
SET ColName = REPLACE(ColName, '"', '');
WHERE ColName SIMILAR TO '%"%'
You can use LIKE operator as well instead of SIMILAR TO.
Since you asked for an efficient way:
UPDATE tbl
SET col = translate(col, '"', '')
WHERE col LIKE '%"%';
Never use SIMILAR TO, its an utterly pointless operator, only included in Postgres for standard compliance. There is hope the standard will include a regular expression match (like ~ in Postgres) instead in the future.
For replacing single characters, translate() is simpler and faster than replace().
If you only want to replace leading and / or trailing characters, use trim() / ltrim() / rtrim() instead. And also change to WHERE col LIKE '"%' OR col LIKE '%"' respectively.

SQL 'LIKE' query using '%' where the search criteria contains '%'

I have an SQL query as below.
Select * from table
where name like '%' + search_criteria + '%'
If search_criteria = 'abc', it will return data containing xxxabcxxxx which is fine.
But if my search_criteria = 'abc%', it will still return data containing xxxabcxxx, which should not be the case.
How do I handle this situation?
If you want a % symbol in search_criteria to be treated as a literal character rather than as a wildcard, escape it to [%]
... where name like '%' + replace(search_criteria, '%', '[%]') + '%'
Use an escape clause:
select *
from (select '123abc456' AS result from dual
union all
select '123abc%456' AS result from dual
)
WHERE result LIKE '%abc\%%' escape '\'
Result
123abc%456
You can set your escape character to whatever you want. In this case, the default '\'. The escaped '\%' becomes a literal, the second '%' is not escaped, so again wild card.
See List of special characters for SQL LIKE clause
The easiest solution is to dispense with "like" altogether:
Select *
from table
where charindex(search_criteria, name) > 0
I prefer charindex over like. Historically, it had better performance, but I'm not sure if it makes much of difference now.
To escape a character in sql you can use !:
EXAMPLE - USING ESCAPE CHARACTERS
It is important to understand how to "Escape Characters" when pattern matching. These examples deal specifically with escaping characters in Oracle.
Let's say you wanted to search for a % or a _ character in the SQL LIKE condition. You can do this using an Escape character.
Please note that you can only define an escape character as a single character (length of 1).
For example:
SELECT *
FROM suppliers
WHERE supplier_name LIKE '!%' escape '!';
This SQL LIKE condition example identifies the ! character as an escape character. This statement will return all suppliers whose name is %.
Here is another more complicated example using escape characters in the SQL LIKE condition.
SELECT *
FROM suppliers
WHERE supplier_name LIKE 'H%!%' escape '!';
This SQL LIKE condition example returns all suppliers whose name starts with H and ends in %. For example, it would return a value such as 'Hello%'.
You can also use the escape character with the _ character in the SQL LIKE condition.
For example:
SELECT *
FROM suppliers
WHERE supplier_name LIKE 'H%!_' escape '!';
This SQL LIKE condition example returns all suppliers whose name starts with H and ends in _ . For example, it would return a value such as 'Hello_'.
Reference: sql/like
Select * from table where name like search_criteria
if you are expecting the user to add their own wildcards...
You need to escape it: on many databases this is done by preceding it with backslash, \%.
So abc becomes abc\%.
Your programming language will have a database-specific function to do this for you. For example, PHP has mysql_escape_string() for the MySQL database.
Escape the percent sign \% to make it part of your comparison value.
May be this one help :)
DECLARE #SearchCriteria VARCHAR(25)
SET #SearchCriteria = 'employee'
IF CHARINDEX('%', #SearchCriteria) = 0
BEGIN
SET #SearchCriteria = '%' + #SearchCriteria + '%'
END
SELECT *
FROM Employee
WHERE Name LIKE #SearchCriteria