Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple like query in sql server return no result

Tags:

sql

sql-server

I've got a simple LIKE query on sql server that returns no result when the leading character is the same as the first...

Database tale contains a row with the field 'Lastname' and the value 'Aalesund'

select * 
from table 
where LastName like 'A%' -- returns no results

if I change the 'Lastname' field to contain 'Alesund' the exact same query returns the correct row.

Any ideas?

Stig

like image 430
Kulvis Avatar asked Jan 25 '12 13:01

Kulvis


2 Answers

In the Danish/Norwegian collation of Sql server, "aa" is considered a "synonym" for å, Since that was how the letter was written in Denmark before the 1940es. This also means that "aa" is considered the last letter of the alphabet when sorting.

I don't know whether this behavior makes sense in a Norwegian context, but I think the collation is being split in two in Sql server 2012, maybe that will fix it.

UPDATE: According to Wikipedia, the usage of "Aa" as "Å" in Norway is similar to the Danish usage: "Aa" has been abolished by the official organizations standardizing the languages for several decades, but some towns refuse to change their names for historical reasons, and "Aa" is also still common in family names.

Based on this, I would say that failing to find "Aalesund" when doing a search for LIKE "A%" is the most correct behavior in a Norwegian-localized application. If you refrain from using the Danish/Norwegian collation in order to change this, you should be aware that it will change other properties such as sorting of special letters, as mentioned above.

The many different collations named Danish_Norwegian_... will not help you AFAIK. They specify behaviour regarding sensitivity to case, accents, character width, and kana type (the latter is only relevant in Japanese, I believe). "Å" is not considered an accented "A", but a distinct letter. A description of the naming conventions for collations is in this MSDN article.

UPDATE 2: Surprisingly, it seems the collation Norwegian_100_CI_AI does what you want. Maybe the usage in Denmark and Norway is different after all...

like image 120
Jonas Høgh Avatar answered Nov 01 '22 11:11

Jonas Høgh


That name is sometimes spelled "Ålesund" The little circle over the A is very small in some fonts. You may be inadvertently copy/pasting "Ålesund" when you try the query with the full name. Try

select *  from table  where LastName like 'Å%'
like image 21
biscuit314 Avatar answered Nov 01 '22 13:11

biscuit314