In SQL how to check if a string contains a substring in(case insensitive)?

StackOverflow https://stackoverflow.com/questions/23314569

  •  10-07-2023
  •  | 
  •  

Question

SELECT * FROM products WHERE name LIKE '%a%'

This works fine when case sensitive, but how to search case insensitively?

Thx ahead

Était-ce utile?

La solution

Simple answer:

SELECT * FROM products WHERE lower(name) LIKE '%a%'

Autres conseils

  SELECT * FROM products WHERE nam LIKE BINARY '%a%'

collate option can also be used and here is how to make it case sensitive from mysql tutorial.

searches for a substring within a larger string you can use CHARINDEX()

If you are using TSQL, you might want to specify a case insensitive collation while doing the search.

e.g.

create table tbl(str varchar(10))

insert into tbl values ('GaP'), ('GAP')

--This will only return 'GaP'
select * from tbl where str collate SQL_Latin1_General_CP1_CS_AS like '%a%'

--This will return both values
select * from tbl where str collate SQL_Latin1_General_CP1_CI_AS like '%a%'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top