Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server LIKE clause issues

Tags:

sql

sql-server

This may something really simple I'm overlooking, but it's driving me nuts.

The basic gist is that I have a very simple table in SQL Server 2014, called Products, which contains some ProductNames column (nvarchar(max)). I put in some test strings, such as

Caly
Calyd
Cali
Cal
Taly
Blical
Blacaly

and I'm executing a very simple SELECT on this table:

SELECT *
FROM Products
WHERE ProductName LIKE '%Cal%'

and here's the thing, I'm only getting back these results

Cali
Cal
Blical

Notice the missing Caly, and Blacaly. But if I search for '%Caly%', I get back the expected results:

Caly
Calyd
Blacaly

Or if I search for '%Ca%', again, as expected:

Caly
Calyd
Cali
Cal
Blical
Blacaly

So really, what gives, I'm at a total loss here. Is 'Cal' some kind of weird easter egg word? Are gnomes eating my results, or what?

(Btw,I tried the same thing out in MySQL, just as a test, it works as expected there.)

like image 610
tamasgobesz Avatar asked Mar 14 '23 12:03

tamasgobesz


2 Answers

You are using Hungarian_CI_AS collation. In Hungarian alphabet digraph 'ly' is a separate letter.

So for example "Caly" is a 3-letter word - 'C', 'a', 'ly'.

like image 75
Jakub Lortz Avatar answered Mar 28 '23 19:03

Jakub Lortz


It's because of your collation, if you look at the Hungarian collation Ly is a character:

Hungarian Collation Chart

And so the like will behave differently, if you really want to treat the data in the Hungarian collated column differently, as in like the Latin1 collation, then you have to tell SQL server this during the select:

SELECT *
FROM Products
WHERE ProductName COLLATE Latin1_General_CI_AS LIKE '%Cal%'
like image 33
steoleary Avatar answered Mar 28 '23 19:03

steoleary