Accent insensitive search using EF6 and SQL Server 2008 R2 - Czech language - sql-server

How to do accent insensitive search query using SQL Server 2008 R2 and EF 6?
I need to do accent insensitive search on let's say user.name column using the Entity framework 6. I have tried to change the collation on column from default Czech_CI_AS to Czech_CI_AI. But it does not work for some Czech letters with wedges like Č, Ř, Š, Ž because the collation treats them as different letters :
http://collation-charts.org/mssql/mssql.0405.1250.Czech_CI_AI.html
I have found similar question here:
How do I perform an accent insensitive compare in SQL Server for 1250 codepage
But the proposed solution using collation Czech_100_CI_AI does not work either (for those special letters).
I have also found few sources how to do it in plain T-SQL. Like this:
SELECT *
FROM [dbo].[User]
WHERE name LIKE '%c%' COLLATE Latin1_General_CI_AI
It works fine. But I do not want to use plain SQL queries. I would like to manage it in an EF way.

I have end up with this solution:
Create view with two columns - one for the search, second for presentation (latin collation will remove some accents from the result).
CREATE VIEW [dbo].[v_UserSearch]
AS
SELECT
dbo.[User].name AS FirstName,
dbo.[User].name COLLATE Latin1_General_CI_AI AS FirstNameCI
FROM dbo.[User]
Create DB mapping for the view in EF context.
Use the FirstNameCI column for the search in EF.
if (!string.IsNullOrWhiteSpace(filter.FirstName))
query = query.Where(x => x.c.FirstNameCI.StartsWith(filter.FirstName));
Use the FirstName column for presentation.

In Entity Framework, when you use Contains() method in where() extension method in IQueryable, it is translated to where clause with like operator in SQL. So I guess this is what are you looking for. You can refer to this SO question.

Related

How do I perform an accent insensitive compare in SQL Server for 1250 codepage

There are already sever question and solution on accent insensitive search on stackoverflow, but none of them work for codepage 1250 (Central European and Eastern European languages).
How do I perform an accent insensitive compare (e with è, é, ê and ë) in SQL Server?
LINQ Where Ignore Accentuation and Case
Ignoring accents in SQL Server using LINQ to SQL
Modify search to make it Accent Insensitive in SQL Server
Questions about accent insensitivity in SQL Server (Latin1_General_CI_AS)
The problem is that accent insensitive collation are bidned to some specific codepages and that I am missing accent insensitive collation for 1250 codepage in MSDN documentation.
I need to modify the collation of the column to make Entity Framework working in accent insensitive way.
For example if I change a collation to SQL_LATIN1_GENERAL_CP1_CI_AI, c with accute is select as c without accute (U+0107) because wrong codepage.
How to solve this?
SELECT *
FROM sys.fn_helpcollations()
WHERE COLLATIONPROPERTY(name, 'CodePage') = 1250
AND description LIKE '%accent-insensitive%';
Returns 264 results to choose from.
Picking the first one
SELECT N'è' COLLATE Albanian_CI_AI
UNION
SELECT N'é'
UNION
SELECT N'ê'
UNION
SELECT N'ë'
returns a single row as desired (showing all compared equal)
OK, it seems that the link MSDN documentation is for SQL server 2008 I use SQL Server 2014, but I was not able to find any collation documentation for 2014.
But the solution is to list the collations from server for my code page:
SELECT name, COLLATIONPROPERTY(name, 'CodePage') AS CodePage
FROM fn_helpcollations()
where COLLATIONPROPERTY(name, 'CodePage') = 1250
ORDER BY name;
And I can see there is a undocumented collation Czech_100_CI_AI which works for me. Heureka!

How to define SQL Server colum name case insensitive but values case sensitive

We just migrated some databases to a new SQL Server 2012 and got some problems with sensitivity.
We would like table & column names to be case insensitive but values should be case sensitive, so
select ... where 'a'='A'
should not return any row, but
select Column from Table
select column from table
should both work.
We tried changing the database (server default) from
Modern_Spanish_CI_AS -> 'a'='A' is true, which we don't want to be, to
Modern_Spanish_CS_AS -> the column/table names must match the defined case
Is there any way to get the desired behavior?
If you choose a case-sensitive collation you must ensure that your your queries are case-sensitive because collation applies to metadata as well as user-data .
You can get round the problem by making the database's collation case-insensitive and using the COLLATE clause when creating tables, or alternatively use a contained database.
Read more about Contained Databases and Contained Database Collations

Doing a linguistic sort in SQL Server 2008

In Oracle, in order to do a linguistic sort, suppose with arabic characters, I use following :
ALTER SESSION SET nls_sort='arabic'
How can I achieve linguistic sorting in SQL Server 2008 ?
SQL Server has the concept of collations which affect ordering and comparison operations.
If your data is configured using a different collation to the one you require, you can force a specific one to sort by in your ORDER BY statement like this:
SELECT *
FROM Table
ORDER BY TextColumn COLLATE Arabic_CI_AS

Inserting Unicode character using asp.net mvc

I have a database field with nVarchar(30). I am using asp.net MVC. When i insert the record in Unicode, i get ?????.
Any one can tell me how can i convert a string to unicode and insert into database.
I am using SQL Server 2008 R2.
Try to change your database collation to Latin1_General_BIN2.
http://msdn.microsoft.com/en-us/library/ms175835.aspx
Make sure:
You Use N' at the start of string literals containing such strings, e.g. N'enović'
If you want to query and ignore accents, then you can add a COLLATE clause to your select. E.g.:
SELECT * FROM Account
WHERE Name = 'enovic' COLLATE Latin1_General_CI_AI

Allow special characters SQL Server 2008

I am using SQL Server 2008 express edition and its collation settings are set to default.I wish to store special characeters like á ,â ,ã ,å ,ā ,ă ,ą ,ǻ in my database but it converts them into normal characters like 'a'. How can I stop SQL Server from doing so?
Make sure that your columns are using the type nvarchar(...), rather than varchar(...). The former is Unicode, the latter is ASCII.
Also, make sure that your database default collation is set to Accent Sensitive, and that your columns are stored that way. You may also want to check your instance default collation, as that affects the default collation for your system databases, particularly tempdb.
Rahul, here is a very simple query that runs perfectly on SQL 2005 and 2008:
Query
DECLARE #t1 TABLE (
Col1 nvarchar(30)
)
INSERT INTO #t1 VALUES (N'á ,â ,ã ,å ,ā ,ă ,ą ,ǻ')
SELECT * FROM #t1
Result
Col1
------------------------------
á ,â ,ã ,å ,ā ,ă ,ą ,ǻ
There is nothing special here. No collation change from default, just a simple NVARCHAR column.
You said you are "just running direct queries in the database". Can you try this query and see if you get the same results?

Resources