SQL LIKE only one letter - sql

When i use LIKE statement in my SQL,
for example,
SELECT * FROM table WHERE name = "%k"
It will return all rows, where name ends on k.
It can return : Ok, OOk, OOOk,
How i can do same statement but with one letter, so it returns only Ok.
Or 2 letters, so it returns only OOk?

_ is a single character wildcard.
SELECT * FROM table WHERE name = `_k`
See this IBM Reference

Use the equality operator (=) or the IN operator instead of the LIKE operator:
SELECT * FROM table WHERE name IN ('Ok', 'OOk')

Use the _ wildcard. It matches only a single character.
_k for Ok.
__k for Ook

depending on the length you provide
For 1 character 'X' followed by k
Select *
from table
where length(name) = 2
AND name = "%k"
For 2 character 'X' followed by k
Select *
from table
where length(name) = 3
AND name = "%k"

You should use an underscore (_) character. See the documentation about operator LIKE.
So, the query you need (1 or 2 chars before k) is:
SELECT * FROM table WHERE name LIKE '_k' OR name LIKE '__k'

Related

Wildcard for string before # character

How would I use a wildcard to filter out any permutation of the following. There can be any number of zeros before the "#" character.
Example
0#test.com
000000#test.com
00000000000#test.com
Basically, I'm looking to wildcard any email address with only zeros before the # character.
Any help would be greatly appreciated
Thank you !
Smiddy
T-SQL:
To remove all values with only zeros before the #
SELECT
*
FROM (SELECT '000#test.com' AS[values] ) [table]
WHERE
LEN(REPLACE((LEFT([values],CHARINDEX('#',[Values])-1)),'0','')) <> 0
-- LEFT & CHARINDEX : get the result LEFT of the #
-- LEN & REPLACE : After all 0 are replaced by '' any email that contained only zeros will result in len = 0.
I would solve it with a regular expression, but the concrete answer depends on your database vendor and the available syntax.
PostgreSQL:
SELECT * FROM table WHERE email ~ '^0+#'
MySQL:
SELECT * FROM table WHERE email REGEXP '^0+#'
Synonyms are email RLIKE '^0+#' and REGEXP_LIKE(email, '^0+#').
Oracle:
SELECT * FROM table WHERE REGEXP_LIKE(email, '^0+#')

Where x character equal value

How can I select records where in the column Value the 5th character is letter A?
For example the following records:
ID Value
-------------------------
1 1234A5636A6363
2 1234A4343B6363
3 1234B5353A6363
if I run
select * from table
where Value like '%A%'
this will return all records
but all I want is the first 2 where the 5th character is A, regardless if there are more A characters in the text or not
select *
from your_table
where substring(Value, 5, 1) = 'A'
The LIKE operator, in addition to %, which matches any number of any character, can use _, which matches any one single character. You may try:
SELECT *
FROM yourTable
WHERE Value LIKE '____A%'; -- 4 underscores here
use like below by using _(underscore)
LIKE '____A%'
SQL Server
select *
from YourTableName
where CHARINDEX('A', ColumnName) = 5
Note:- This finds where string 'A' starts at position 5
AND specify Your ColumnName

Select Where Like regular expression

I need to create a SQL Query.
This query need to select from a table where a column contains regular expression.
For example, I have those values:
TABLE test (name)
XHRTCNW
DHRTRRR
XHRTCOP
CPHCTPC
CDDHRTF
PEOFOFD
I want to select all the data who have "HRT" after 1 char (value 1, 2 and 3 - Values who looks like "-HRT---") but not those who might have "HRT" after 1 char (value 5).
So I'm not sure how to do it because a simple
SELECT *
FROM test
WHERE name LIKE "%HRT%"
will return value 1, 2, 3 and 5.
Sorry if I'm not really clear with what I want/need.
You can also change the pattern. Instead of using % which means zero-or-more anything, you can use _ which means exactly one.
SELECT * FROM test WHERE name like '_HRT%';
You can use substring.
SELECT * FROM test WHERE substring(name from 2 for 3) = 'HRT'
Are the names always 7 letters? Do:
SELECT substring (2, 4, field) from sometable
That will just select the 2-4th characters and then you can use like "%HRT"

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.

WHERE clause that finds in 123xxx digits

I try to search all rows that contain
123000 - 123xxx
So the last three are unknown but must be filled up.
So I dont want to find for example
12300 or 1230000
select * from your_table
where some_column between 123000 and 123999
If the value is a number, then just use:
where value >= 123000 and value < 124000
If value is a string:
where char_length(value) = 6 and left(value, 3) = '123'
For Firebird version 2.1 and above
select * from table_name
where
(BIN_AND(table_name.field_name,123000) = 123000)
in stored procedure with parameter
select * from table_name
where
(BIN_AND(table_name.field_name,:p) = :p)
If your sure the field will always be an int, you could use
WHERE Field LIKE '123___'
Doc for _ can be found here: here
In short, _ is a single wildcard character, so on a field which can only be an int, this should match 123000 to 123999
This could also be useful if you have to find any reference to 123 where the last 3 values could be anything, including characters
Regards,
Jamie