Search using English characters to match foreign ones - asp.net

net MVC application using EF6
and would like searching for "O" to return matches containing "Ø"
I am using the default collation in MSSQL Server at the moment.
Do I have to change the collation or add some code?
thanks

That's called normalization.
E.g. køpenhavn could be written as
køpenhavn
kopenhavn
koepenhavn
So from a plain-sql you could query
select ...
where col1='kopenhavn'
or col1=replace('kopenhavn','o','ø')
or col1=replace('kopenhavn','o','oe')
or
select ...
where col1='kopenhavn' COLLATE SQL_Latin1_General_CP1253_CI_AI
(returns only 2 of 3).
The Entity Framework does support a COLLATE clause, but LINQ to SQL does not.

changing the collation
ALTER TABLE Resource ALTER COLUMN FirstName
nvarchar(50)COLLATE Latin1_General_CI_AI;
worked

Related

SQLite ordering column with accented characters

I'm developping a website using SQLite databases with PHP. I'm running Windows (dev) and my production environment should be a *nix platform. Here is the schema of my table :
CREATE TABLE [animals](
[id] INTEGER NOT NULL UNIQUE,
[name] VARCHAR(50) NOT NULL
);
I want to sort the animals by name (contains accented characters). My SQL query is:
SELECT * FROM animals ORDER BY name DESC
and I get :
éléphant
tigre
renard
chien
instead of,
tigre
renard
éléphant
chien
I've searched on the web. I tried,
SELECT * FROM animals ORDER BY name COLLATE *binary|nocase|rtrim* DESC
but I have the same problem. I also tried,
SELECT * FROM animals ORDER BY name COLLATE *localized|unicode* DESC
But I get an error (either my SQLite client crashes, either PHP returns the following error: no such collation sequence: UNICODE).
It seems there's a solution for Android, but I'm running Windows and my production environment should be a *nix platform.
How can I get my animals sorted the right way?
By default, SQLite has only ASCII collations.
It would be possible to use the ICU extension to get Unicode support, but PHP did not enable this.
But you can create your own collation and implement the comparison in PHP.

Using COLLATE on peewee queries

I found in the peewee documentation how to create custom collations, but I wasn't able to find how to use the built-in sqlite collating sequences.
Ho do I create the following query in peewee (it is the last one in the above mentioned sqlite documentation page)?
SELECT x FROM t1 ORDER BY c COLLATE NOCASE, x;
And how do I specify the collation for an index?
CREATE INDEX i1 ON t1(f1 COLLATE NOCASE);
EDIT
The answer from coleifer addresses the question about the query.
For the index creation I am using the following trick, which works well when you create the indexes only once at startup (like in my app).
The case insensitive unique index on two columns on the table LockedFiles prevents duplicated entries.
class LockedFiles(PeeweeModel):
folder = peewee.CharField(index=True)
file = peewee.CharField(index=True)
#classmethod
def custom_init(cls):
db.execute_sql('create unique index if not exists lockedfiles_unique '
'on lockedfiles(folder collate nocase, file collate nocase)', {})
def create_tables(drop_existing_tables):
for table in [LockedFiles, Model2, Model3]:
if drop_existing_tables:
table.drop_table(True)
table.create_table(True)
try:
table.custom_init()
except:
pass
create_tables(drop_existing_tables=False)
You can specify a collation by building up the SQL clause and passing it to order_by().
For example:
collated = Clause(MyModel.field, SQL('COLLATE NOCASE'))
MyModel.select().order_by(collated, MyModel.other_field)
For the index unfortunately you will need to create that by hand as peewee does not know how to add collation information to the CREATE INDEX SQL. If you'd like to open a pull-request I would definitely consider merging that feature, though.

Can we force SQL Server for case sensitivity

What if I want to authenticate user using Stored Procedure using ADO.Net? Is there any way to do this or directly authenticate or find other way?
You can use database collation for your idea.
you must use SQL_Latic1_General_CP1_CS_AS instead of you must use SQL_Latic1_General_CP1_CI_AS
Also you can apply your collation on the special columns.
For example :
CREATE TABLE yourtable
(Name NvarChar(100) COLLATE SQL_Latic1_General_CP1_CI_AS, ...

Error: no such collation sequence: NOCASE_UTF8

I m using an existing db file in sqlite3. One of the column in a particular table is having clause COLLATE NOCASE_UTF8. I m not able to insert any text in that field. its giving following error:
sqlite> INSERT INTO recording (recordingName) VALUES ('abcd');
Error: no such collation sequence: NOCASE_UTF8
How should I enter the text in it?
That's a custom collation. SQLite has native support for a collation named "NOCASE", but not for a collation named "NOCASE_UTF8". You're getting the error, I think, because there is no such collation defined in your SQLite database, but nevertheless that collation name is still stored as part of the table definition.
I'm not sure how SQLite handles storage of collations defined through the C API. But I'm pretty sure that's what you need to fix. See also the docs for Collation Needed Callbacks. (You might need only one call to sqlite3_collation_needed().)

Syntax for using collate nocase in a SQLite replace function

I have an existing database where they created theiw own unicode collation sequence. I'm trying to use the following code and get a "no such collation sequence" exception. Can anybdy hlep with the the syntax to use "collate nocase" with this code?
update Songs set
SongPath = replace (SongPath, 'Owner.Funkytown', 'Jim');
Dump database (via shell), edit output SQL (find and change column definitions, set COLLATION NOCASE). Recreate database.

Resources