SQL LIKE with special characters - sql

If I try to create a select with special (nordic) characters like:
Select * from users where name like '%æ%'
It just selects all users instead of those containing the letter 'æ'.
Do I need to install some special drivers to the database, or is there something else I have missed?
Update:
I am using a SQL Server 2008 database, the collation is set to 'SQL_Latin1_General_CP1_CI_AS' and the datafield is a nullable nVarChar datatype.

Most likely some collation or datatype issue
Example, gives 97 and 230
SELECT ASCII('æ' COLLATE Albanian_CI_AI), ASCII('æ' COLLATE Latin1_General_CI_AS)
We'll need more info basically.
Edit: Question about Danish/Norwegian å (although unresolved)
Edit 2: change the code to this if name is nvarchar so the literal becomes unicode too.
Select * from users where name like N'%æ%'

Make sure you're using an extended character set (UTF-8) and you should be fine.

it should be work correctly ..
but try this Select * from users where name like '%[æ]%'

Related

PostgreSQL pattern match query does not work as expected [duplicate]

I looked around some and didn't find what I was after so here goes.
SELECT * FROM trees WHERE trees.`title` LIKE '%elm%'
This works fine, but not if the tree is named Elm or ELM etc...
How do I make SQL case insensitive for this wild-card search?
I'm using MySQL 5 and Apache.
I've always solved this using lower:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE '%elm%'
SELECT *
FROM trees
WHERE trees.`title` COLLATE UTF8_GENERAL_CI LIKE '%elm%'
Actually, if you add COLLATE UTF8_GENERAL_CI to your column's definition, you can just omit all these tricks: it will work automatically.
ALTER TABLE trees
MODIFY COLUMN title VARCHAR(…) CHARACTER
SET UTF8 COLLATE UTF8_GENERAL_CI.
This will also rebuild any indexes on this column so that they could be used for the queries without leading '%'
The case sensitivity is defined in the columns / tables / database collation settings. You can do the query under a specific collation in the following way:
SELECT *
FROM trees
WHERE trees.`title` LIKE '%elm%' COLLATE utf8_general_ci
for instance.
(Replace utf8_general_ci with whatever collation you find useful). The _ci stands for case insensitive.
This is the example of a simple LIKE query:
SELECT * FROM <table> WHERE <key> LIKE '%<searchpattern>%'
Now, case-insensitive using LOWER() func:
SELECT * FROM <table> WHERE LOWER(<key>) LIKE LOWER('%<searchpattern>%')
Simply use :
"SELECT * FROM `trees` WHERE LOWER(trees.`title`) LIKE '%elm%'";
Or Use
"SELECT * FROM `trees` WHERE LCASE(trees.`title`) LIKE '%elm%'";
Both functions works same
I'm doing something like that.
Getting the values in lowercase and MySQL does the rest
$string = $_GET['string'];
mysqli_query($con,"SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER('%$string%')");
And For MySQL PDO Alternative:
$string = $_GET['string'];
$q = "SELECT *
FROM table_name
WHERE LOWER(column_name)
LIKE LOWER(?);";
$query = $dbConnection->prepare($q);
$query->bindValue(1, "%$string%", PDO::PARAM_STR);
$query->execute();
use ILIKE
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
it worked for me !!
Non-binary string comparisons (including LIKE) are case insensitive by default in MySql:
https://dev.mysql.com/doc/refman/en/case-sensitivity.html
I think this query will do a case insensitive search:
SELECT * FROM trees WHERE trees.`title` ILIKE '%elm%';
You don't need to ALTER any table. Just use the following queries, prior to the actual SELECT query that you want to use the wildcard:
set names `utf8`;
SET COLLATION_CONNECTION=utf8_general_ci;
SET CHARACTER_SET_CLIENT=utf8;
SET CHARACTER_SET_RESULTS=utf8;
well in mysql 5.5 , like operator is insensitive...so if your vale is elm or ELM or Elm or eLM or any other , and you use like '%elm%' , it will list all the matching values.
I cant say about earlier versions of mysql.
If you go in Oracle , like work as case-sensitive , so if you type like '%elm%' , it will go only for this and ignore uppercases..
Strange , but this is how it is :)
SELECT name
FROM gallery
WHERE CONVERT(name USING utf8) LIKE _utf8 '%$q%'
GROUP BY name COLLATE utf8_general_ci LIMIT 5
You must set up proper encoding and collation for your tables.
Table encoding must reflect the actual data encoding. What is your data encoding?
To see table encoding, you can run a query SHOW CREATE TABLE tablename
When I want to develop insensitive case searchs, I always convert every string to lower case before do comparasion
I've always solved like this:
SELECT * FROM trees WHERE LOWER( trees.title ) LIKE LOWER('%elm%');
For example if you want to search name like Raja not raja, Royal not royal etc, add BINARY before column name in WHERE clause.
SELECT name FROM person_tbl
WHERE BINARY name LIKE "R%";

SQL query with german special char and Latin1_General_CI_AS

I am trying to read some entries out of a microsoft sql database.
The problem ist, that one of the columns has a name with special characters "bedürfnisse" and the entries are all in Latin1_General_CI_AS.
I need to select this column.
Select nameName.bedürfnisse
FROM nameName
This is not working. I also tried
Select nameName.bedürfnisse COLLATE Latin1_General_CI_AS
FROM nameName
but this is also not working. How can i select this column?
You need to escape your column names if they contain special characters. This also includes whitespace or other non-standard ascii characters in column names.
For standard ANSI SQL the syntax to do this is:
SELECT nameName."bedürfnisse"
FROM nameName
For SQL Server this also works:
SELECT nameName.[bedürfnisse]
FROM nameName
For MySQL, this also works:
SELECT nameName.`bedürfnisse`
FROM nameName
What really helped was to define UTF-8 in the connection. Its one of the option parameter

how to retrieve sql column includes special characters and alphabets

How to retrieve a column containing special characters including alphabets in SQL Query. i have a column like this 'abc%def'. i want to retrieve '%' based columns from that table.
Please help me in this regard.
Is abc%def the column name? or column value? Not sure what you are asking but if you mean your column name contains special character then you can escape them which would be different based on specific RDBMS you are using
SQL Server use []
select [abc%def] from tab
MySQL use backquote
select `abc%def` from tab
EDIT:
Try like below to fetch column value containing % character (Checked, it works in Ingres as well)
select * from tab where col like '%%%'
Others suggest that like '%%%' works in Ingres. So this is something special in Ingres. It does not work in other dbms.
In standard SQL you would have to declare an escape character. I think this should work in Ingres, too.
select * from mytable where str like '%!%%' escape '!';

Unicode characters in Sql table

I am using Sql Server 2008 R2 Enterprise. I am coding an application capable of inserting, updating, deleting and selecting records from a Sql tables. The application is making errors when it comes to the records that contain special characters such as ć, č š, đ and ž.
Here's what happens:
The command:
INSERT INTO Account (Name, Person)
VALUES ('Boris Borenović', 'True')
WHERE Id = '1'
inserts a new record but the Name field is Boris Borenovic, so character ć is changed to c.
The command:
SELECT * FROM Account
WHERE Name = 'Boris Borenović'
returns the correct record, so again the character ć is replaced by c and the record is returned.
Questions:
Is it possible to make Sql Server save the ć and other special characters mentioned earlier?
Is it still possible, if the previous question is resolved, to make Sql be able to return the Boris Borenović record even if the query asks for Boris Borenovic?
So, when saving records I want Sql to save exactly what is given, but when retrieving the records, I want it to be able to ingnore the special characters. Thanks for all the help.
1) Make sure the column is of type nvarchar rather than varchar (or nchar for char)
2) Use N' at the start of string literals containing such strings, e.g. N'Boris Borenović'
3) If you're using a client library (e.g. ADO.Net), it should handle Unicode text, so long as, again, the parameters are marked as being nvarchar/nchar instead of varchar/char
4) 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 = 'Boris Borenovic' COLLATE Latin1_General_CI_AI
Where _CI_AI means Case Insensitive, Accent Insensitive, should return all rows with all variants of the "c" at the end.
5) If the column in the table is part of a UNIQUE/PK constraint, and you need it to contain both "Boris Borenović" and "Boris Borenovic", then add a COLLATE clause to the column definition, but this time use a collation with "_AS" at the end, which says that it's accent sensitive.
To allow SQL Server to store special characters, use nvarchar instead of varchar for the column type.
When retrieving, you can force a accent-insensitve collation so that it ignores the different C's:
WHERE Name = 'Boris Borenović' COLLATE Cyrillic_General_CI_AI
Here, CI stands for Case Insensitive, and AS for Accent Insensitive.
I've faced with the same problem and after some researching:
https://dba.stackexchange.com/questions/139551/how-do-i-set-a-sql-server-unicode-nvarchar-string-to-an-emoji-or-supplementary
What is the difference between varchar and nvarchar?
I altered type of needed fields:
ALTER TABLE [table_name] ALTER COLUMN column_name [nvarchar]
GO
And it works!

SQL Server - Searching string with international characters using LIKE clause

I have a field 'Description' which can have product descriptions with any unicode characters.
If I search for a description which contains an international character, with a LIKE condition (word searched with does not have the international character) I get the following results:
Ex: GEWÜRZTRAMINER is one of the descriptions.
When I do:
Select * from table where Description LIKE '%GEWURZTRAMINER%', it retrieves the entry.
When I do:
Select * from table where Description LIKE '%GEWURZ%', the entry is not retrieved.
(Note: the search condition does not include the Ü but has a U)
Is there a way around this so that I can retrieve with '%GEWURZ%' as well?
For bog standard varchar, you'd have to coerce to a accent insensitive collation
Select 1 where 'GEWÜRZTRAMINER' COLLATE LATIN1_GENERAL_CI_AI LIKE '%GEWURZTRAMINER%'
There should be no difference between the calls though for the SQL you provided.
It will depend on the collation order for the column. It should work if you use e.g. SQL_Latin1_General_CP1_CI_AI