Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql search on fields containing diacritics

I'm working at a web site search which uses Sql server 2008 express edition.

I have 'Books' table which has a field named 'Title' where there are titles containing romanian (latin) characters.

Well, if the user inserts a word like: 'casa' in the search field and i have a title in db like: 'test casă test' i want to show that book but:

select * from books where title like '%casa%'

will not find it...

Do you know any function which removes the diacritics when doing the select?

I know that i can solve the problem with full text searching add-on of sql server but i want to do it in a simpler way.

like image 412
Cristian Boariu Avatar asked Aug 12 '10 15:08

Cristian Boariu


People also ask

What special characters are not allowed in SQL query?

Names can contain only alphanumeric characters and must begin with an alphabetic character or an underscore (_). Database names must begin with an alphabetic character, and cannot begin with an underscore.

Is there a Contains function 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.


2 Answers

select * from books where title COLLATE Latin1_General_CI_AI like '%casa%'

Of course, you should choose a collation that matches yours... just change AS to AI on the end to make it "accent insensitive"

Quick example with German umlauts

DECLARE @foo TABLE (bar varchar(100) /*implied COLLATE Latin1_General_CI_AS*/)

INSERT @foo VALUES ('xxx fish yyy')
INSERT @foo VALUES ('xxx bar yyy' )
INSERT @foo VALUES ('xxx bär yyy')

select * from @foo where bar COLLATE Latin1_General_CI_AI like '%bar%'
select * from @foo where bar like '%bar%'
like image 183
gbn Avatar answered Nov 15 '22 16:11

gbn


You need to use an accent insensitive collate clause.

like image 20
Martin Smith Avatar answered Nov 15 '22 15:11

Martin Smith