Doing a linguistic sort in SQL Server 2008 - sql-server

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

Related

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

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.

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

2 different collations conflict when merging tables with Sql Server?

I have DB1 which has a Hebrew collation
I also have DB2 which has latin general collation.
I was asked to merge a table (write a query) between DB1.dbo.tbl1 and DB2.dbo.tbl2
I could write in the wuqery
insert into ...SELECT Col1 COLLATE Latin1_General_CI_AS...
But I'm sick of doing it.
I want to make both dbs/tables to the same collation so I don't have to write every time COLLATE...
The question is -
Should I convert latin->hebrew or Hebrew->latin ?
we need to store everything from everything. ( and all our text column are nvarachr(x))
And if so , How do I do it.
If you are using Unicode data types in resulted database - nvarchar(x), then you are to omit COLLATE in INSERT. SQL Server will convert data from your source collation to Unicode automatically. So you should not convert anything if you are inserting to nvarchar column.

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?

How do I change SQL Server 2005 to be case sensitive?

I hate case sensitivity in databases, but I'm developing for a client who uses it. How can I turn on this option on my SQL Server, so I can be sure I've gotten the case right in all my queries?
You don't actually need to change the collation on the entire database, if you declare it on the table or columns that need to be case-sensitive. In fact, you can actually append it to individual operations as needed.
SELECT name WHERE 'greg' = name COLLATE Latin1_GENERAL_CS_AS
I know, you said that you want this to apply throughout the database. But I mention this because in certain hosted environments, you can't control this property, which is set when the database is created.
How about:
ALTER DATABASE database_name COLLATE collation_name
See BOL for a list of collation options and pick the case-sensitive one that best fits your needs (i.e. the one your client is using).
Obviously, it's probably a good idea to make a full backup of your database before you try this. I've never personally tried to use a database with a different collation than the server's default collation, so I don't know of any "gotchas". But if you have good backups and test it in your environment before deploying it to your client, I can't imagine that there's much risk involved.
If you have a DB that has a different collation to the instance default, you can run into problems when you try and join your tables with temporary ones. Temporary tables have to collation of the instance (because they're system objects) so you need to use the COLLATE database_default clause in your joins.
select temp.A, table.B
from #TEMPORARY_TABLE temp inner join table
on temp.X COLLATE database_default = table.Y
This forces the collation of temp.X (in this example) to the collation of the current DB.
You'll have to change the database collation. You'll also need to alter the table and column level collation. I beleive you can find a script out there if you google it.

Resources