How do I get a query that brings me word that contains or does not the special character?
Eg, I have this data: "NÃO" and if I search by typing "NAO", you should return this information to me. And the converse too, if I have: "ANTONIO" and I write "ANTÓNIO," ANTÓNIO should return to me.
I use this code but it does not work:
SELECT * FROM PESSOA WHERE NOME like '%'+ #PROCURAR + '%'
Accent Sensitive and Accent Insensitive searching can be don by using Latin1_general_CI_AI
ie, ÃNTONIO and ANTONIO are the same if Accent Insensitive.
In the below query Latin1_general_CI_AI can be break down into the following parts.
latin1 makes the server treat strings using charset latin 1, basically ascii.
CI specifies case-insensitive, so "ABC" equals to "abc".
AI specifies accent-insensitive,so 'ü' equals to 'u'.
Your query should be as follows:
SELECT * FROM table_name WHERE field_name COLLATE Latin1_general_CI_AI Like '%ANTONIO%' COLLATE Latin1_general_CI_AI
Expected Result is as follows:
Id name
1 ÃNTONIO
2 ANTÓNIO
3 ANTONIO
4 ANTÓNIÓ
SELECT *
FROM PESSOA
WHERE NOME COLLATE Latin1_General_CI_AI Like '%'+ #PROCURAR + '%'
COLLATE Latin1_General_CI_AI
for please visit Latin1_General_CI_AI
in sql server, what is: Latin1_General_CI_AI versus Latin1_General_CI_AS
see also
https://www.mssqltips.com/sqlservertip/4395/understanding-the-collate-databasedefault-clause-in-sql-server/
Related
I'm using SQL Server 2019 with graph tables. My script returns only Id9 while the same script under MySQL returns ID4,8,9 (which is the expected result). I don't know why the '%' used as prefix is not working on SQL Server.
Here's the script:
CREATE TABLE Test([Id] int IDENTITY(1,1) NOT NULL,
[FullName] nvarchar(128) NULL,
CONSTRAINT [PK_Test_Id] PRIMARY KEY CLUSTERED (Id ASC));
INSERT INTO Test (FullName) VALUES
(N'F''Sheree Jones II' ),
(N'Cybèle' ),
(N'T''Junior' ),
(N'Urane Of Watson Lake' ),
(N'J''Sirène De Dan Jourdain' ),
(N'Goodson des loups de l''antarctique'),
(N'F''Sheree Jones Of Dawson City' ),
(N'Pénélope Of Watson Lake' ),
(N'Liubov''s Siren' ),
(N'Siréna' );
select * from Test where FullName like '%siren%'
#NolmëInformatique, this is the variant with table-valued function usage:
create function schema_name.function_name (#name nvarchar(100))
returns table
as
return
(
select FullName from Test
WHERE FullName COLLATE Latin1_general_CI_AI Like '%'+#name+'%'
COLLATE Latin1_general_CI_AI
);
The function can be called via this query:
select * from schema_name.function_name ('siren');
Because a letter with accent is not the same letter without one. And you should consider uppercase letters. Try this code:
select * from Test WHERE upper(FullName) COLLATE Latin1_general_CI_AI Like '%CAFE%' COLLATE Latin1_general_CI_AI
This is in addition to #Vad1m's answer above.
The 'Siren' in your table has a Capitalized 'S'.
Check if server is case insensitive or not. Run this query:
SELECT SERVERPROPERTY('COLLATION')
If result is:
SQL_Latin1_General_CP1_CI_AS
CI means case insensitive.
If not, you will need to use lower() function in your where clause.
Parent question - Thanks to Iamdave, part of the problem is solved. Now the challenge is to make the search case insensitive in the db where the following collation is set already: COLLATE Latin1_General_CS_AS
I am using this query and it is not working - couldn't match test, Test, could match only TEST
UPDATE dbo.BODYCONTENT
SET BODY = LTRIM(RTRIM(REPLACE(
REPLACE(
REPLACE(N' ' + CAST(BODY AS NVARCHAR(MAX))
+ N' ', ' ', '<>'), '>TEST<', '>Prod<'), '<>', ' ')))
FROM dbo.BODYCONTENT
WHERE BODY COLLATE Latin1_General_CI_AS LIKE '%TEST%' COLLATE Latin1_General_CI_AS;
How to make the search string in the replace function to match case insensitive
Other queries and results:
UPDATE dbo.BODYCONTENT SET BODY =
ltrim(rtrim(replace(replace(
replace(N' ' + cast(BODY as nvarchar(max)) + N' ' ,' ','<>')
,'>Test<','>Prod<),'<>',' ')))
from dbo.BODYCONTENT WHERE lower(BODY) like '%test%';
result: Argument data type ntext is invalid for argument 1 of lower function.
Based on the comments, it'd be easier to just use LOWER
where lower(body) like '%test%'
What you have there should work, unless there's some assumption that's being left out of the question (such as not actually being collated like you think, or the test rows actually being absent.
You can do this a couple ways. As scsimon pointed out, you could simply do a lower case comparison. That's probably the most straight forward.
You can also explicitly collate the column like you're doing. You shouldn't need to specifically collate the '%TEST%' string though (unless I'm mistaken; on my machine it wasn't necessary. I suppose default DB settings might negate this argument).
Finally, another option is to have a computed column on the table which is the case insensitive version of the field. That's essentially the same as the previous method, but it's part of the table definition instead.
declare #t table
(
body nvarchar(max) collate Latin1_General_CS_AS,
body_Insensitive as body collate Latin1_General_CI_AS
)
insert into #t
values ('test'), ('Test'), ('TEST')
select * from #t where BODY collate Latin1_General_CI_AS like '%test%' collate Latin1_General_CI_AS;
select * from #t where lower(body) like '%test%'
select * from #T where body_Insensitive like '%TeSt%'
So I've been using the query builder in visual studio (visual basic) to search a local mdb file. I have a button that is clicked to make a prompt for a search, and it works fine except that is is not case sensitive. Here is what I have so far:
SELECT ID, LastName, FirstName, FullTime, HireDate, Salary
FROM SalesStaff
WHERE LastName like ? + '%'
My professor wants us to use the InStr fuction, but how do I get that to work with a prompt?
(InputBox in my vb form code). Furthermore, it doesn't seem to have case sensitivity either. This is the first time I am using SQL, so I hardly know what I'm doing.
Thanks in advance!
You can amend the collation settings of your database/table.
Alternatively if you just want a case sensitive comparison on this one statement you can use the collate keyword, such as below:
select 1 where 'abc' = 'ABC'
select 1 where 'abc' collate Latin1_General_CS_AS = 'ABC' collate Latin1_General_CS_AS
select 1 where 'abc' collate Latin1_General_CI_AS = 'ABC' collate Latin1_General_CI_AS
select 1 where upper('abc') collate Latin1_General_CS_AS = 'ABC' collate Latin1_General_CS_AS
select 1 where upper('abc') collate Latin1_General_CI_AS = 'ABC' collate Latin1_General_CI_AS
CI stands for case insensitive.
CS stands for case sensitive.
I followed this post How do I perform an accent insensitive compare (e with è, é, ê and ë) in SQL Server? but it doesn't help me with " ş ", " ţ " characters.
This doesn't return anything if the city name is " iaşi " :
SELECT *
FROM City
WHERE Name COLLATE Latin1_general_CI_AI LIKE '%iasi%' COLLATE Latin1_general_CI_AI
This also doesn't return anything if the city name is " iaşi " (notice the foreign ş in the LIKE pattern):
SELECT *
FROM City
WHERE Name COLLATE Latin1_general_CI_AI LIKE '%iaşi%' COLLATE Latin1_general_CI_AI
I'm using SQL Server Management Studio 2012.
My database and column collation is "Latin1_General_CI_AI", column type is nvarchar.
How can I make it work?
The characters you've specified aren't part of the Latin1 codepage, so they can't ever be compared in any other way than ordinal in Latin1_General_CI_AI. In fact, I assume that they don't really work at all in the given collation.
If you're only using one collation, simply use the correct collation (for example, if your data is turkish, use Turkish_CI_AI). If your data is from many different languages, you have to use unicode, and the proper collation.
However, there's an additional issue. In languages like Romanian or Turkish, ş is not an accented s, but rather a completely separate character - see http://collation-charts.org/mssql/mssql.0418.1250.Romanian_CI_AI.html. Contrast with eg. š which is an accented form of s.
If you really need ş to equal s, you have replace the original character manually.
Also, when you're using unicode columns (nvarchar and the bunch), make sure you're also using unicode literals, ie. use N'%iasi%' rather than '%iasi%'.
In SQL Server 2008 collations versioned 100 were introduced.
Collation Latin1_General_100_CI_AI seems to do what you want.
The following should work:
SELECT * FROM City WHERE Name LIKE '%iasi%' COLLATE Latin1_General_100_CI_AI
Not tidiest solution I guess, but if you know that it's just the "ş" and "ţ" characters that are the problem, would it be acceptable to do a replace?
SELECT *
FROM City
WHERE replace(replace(Name,'ş','s'),'ţ','t') LIKE COLLATE Latin1_general_CI_AI '%iasi%' COLLATE Latin1_general_CI_AI
You just need to change collation of name field before like operation. Check test code below
DECLARE #city TABLE ( NAME NVARCHAR(20) )
INSERT INTO #city
VALUES ( N'iaşi' )
SELECT *
FROM #city
WHERE name LIKE 'iasi'
--No return
SELECT *
FROM #city
WHERE name COLLATE Latin1_general_CI_AI LIKE '%iasi%'
--Return 1 row
This problem was haunting me for some time, until now, when I've finally figured it out.
Presuming your table or column is of SQL_Latin1_General_CP1_CI_AS collation, if you do:
update
set myCol = replace(myCol , N'ș', N's')
from MyTable
and
update
set myCol = replace(myCol,N'ț',N't')
from MyTable
the replace function will not find these characters, because the "ș" made from your keyboard (Romanian Standard keyboard) differs from the "ş" or "ţ" found in your database.
As a comparison: ţț and şș - you can see that they differ because the accents are closer to the "s" or "t" character.
Instead, you must do:
update
set myCol = replace(myCol , N'ş', N's')
from MyTable
and
update
set myCol = replace(myCol,N'ţ',N't')
from MyTable
I have a series of tables that each have a dedicated column to a foreign language. Languages vary from Japanese, Thai, English, Italian, French, more than 20 in all.
All of these tables are set up with Latin Case Insensitive collation. DB works fine.
But now I am trying to query against the specific foreign language column of each table. Lets take Japanese for starters. I'd like a foreign language user to enter foreign text and find the record based on the foreign language column.
DECLARE #myVar nvarchar(max);
SET #myVar = 'エンジン ストップ リレー' = 'Engine Stop Relay' in english
Select *
FROM tableJapanese
WHERE langString = #myVar;
I have tried a multitude of collation combinations. I even copied the table and changed the collation of the column to Japanese_CI_AI and tried to query it that way.
None of these WHERE clauses work on either table/columm collation, when the column was Latin or Japanese...
WHERE lang_String collate Japanese_CI_AI = #myVar;
WHERE lang_String = #myVar collate Japanese_CI_AI;
WHERE lang_String collate Japanese_CI_AI = #myVar collate Japanese_CI_AI;
WHERE lang_String collate Japanese_CI_AI = #myVar;
WHERE lang_String = #myVar collate Japanese_CI_AI;
WHERE lang_String collate Japanese_CI_AI = #myVar collate Japanese_CI_AI;
I would like to leave the columns/database as Latin collation and code the queries for each language if possible.
This seems like one of those problems that if were a snake I'd been bitten already. Can anyone see what I am missing?
MSSQL Express 2008 R2
SOLUTION:
Add N in front of the field, it indicates unicode to SQL...
Select *
FROM tblLangJAP_test
WHERE lang_String = N'エンジン ストップ リレー';
Works flawlessly.
Thanks,
Add N in front of the field, it indicates unicode to SQL...
Select *
FROM tblLangJAP_test
WHERE lang_String = N'エンジン ストップ リレー';
Works flawlessly.