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
Related
I am using SQL Server 2012 and I need to perform a search on a specific field, called Notes. The search criteria is to find all rows where the term 8% is mentioned in that specific field.
The WHERE clause of my T-SQL query looks like this:
WHERE [Notes] like '%[8%]%'
However, the query is not filtering correctly based on the above syntax. It is also including rows where the term 8 is mentioned.
I had a look at the answers proposed in the question below, but they are still not giving me the correct answer.
SQL 'LIKE' query using '%' where the search criteria contains '%'
A single character class represents a single character. So [%] means a literal percent symbol, and 8[%] means literal 8%. Try this:
SELECT * FROM yourTable WHERE [Notes] like '%8[%]%'
Demo
you need to escape % in query for example below
SELECT columns FROM table
WHERE column LIKE '%\%%' ESCAPE '\'
Using SQL Escape Sequences
Below is from MSDN
Pattern Matching with the ESCAPE Clause
You can search for character strings that include one or more of the special wildcard characters. For example, the discounts table in a customers database may store discount values that include a percent sign (%). To search for the percent sign as a character instead of as a wildcard character, the ESCAPE keyword and escape character must be provided. For example, a sample database contains a column named comment that contains the text 30%. To search for any rows that contain the string 30% anywhere in the comment column, specify a WHERE clause such as WHERE comment LIKE '%30!%%' ESCAPE '!'. If ESCAPE and the escape character are not specified, the Database Engine returns any rows with the string 30.
you can try below answer given by #TimBiegeleisen that is also easy way.
just change your where clause as
WHERE `Notes` LIKE '%8!%%' ESCAPE '!
I want to select rows where column [Name] contains ONLY Cyrillic characters in UPPERCASE, and comma and hyphen from the table using LIKE :
SELECT *
FROM Clients
WHERE NAME LIKE '%[А-Я][,-]%' COLLATE Cyrillic_General_CS_AS
Or using explicit pattern:
SELECT *
FROM Clients
WHERE NAME LIKE '%[АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ][,-]%' COLLATE Cyrillic_General_CS_AS
But these selects rows in which at least one character exists in pattern (but allows any other characters not exists in pattern).
Maybe using ^ (NOT predicate) excluding any other characters like this:
SELECT *
FROM Clients
WHERE NAME LIKE '%[^A-Z][./=+]%' COLLATE Cyrillic_General_CS_AS
But this requires enumeration a large number of unnecessary characters.
How best to make a selection?
Use a double negative. Search for rows where the column doesn't contain at least one character not in the set you're interested in:
SELECT *
FROM Clients
WHERE NAME NOT LIKE '%[^-АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ,]%' COLLATE Cyrillic_General_CS_AS
(I'm not quite sure what you were attempting to do by placing the hyphen and comma in a separate grouping, but I've moved them into the same group for now, since that seems to make some sense)
I need to find the '&' in a string.
SELECT * FROM TABLE WHERE FIELD LIKE ..&...
Things we have tried :
SELECT * FROM TABLE WHERE FIELD LIKE '&&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&\&&'
SELECT * FROM TABLE WHERE FIELD LIKE '&|&&' escape '|'
SELECT * FROM TABLE WHERE FIELD LIKE '&[&]&'
None of these give any results in SQLServer.
Well some give all rows, some give none.
Similar questions that didn't work or were not specific enough.
Find the % character in a LIKE query
How to detect if a string contains special characters?
some old reference Server 2000
http://web.archive.org/web/20150519072547/http://sqlserver2000.databases.aspfaq.com:80/how-do-i-search-for-special-characters-e-g-in-sql-server.html
& isn't a wildcard in SQL, therefore no escaping is needed.
Use % around the value your looking for.
SELECT * FROM TABLE WHERE FIELD LIKE '%&%'
Your statement contains no wildcards, thus is equivalent to WHERE FIELD = '&'.
& isn't a special character in SQL so it doesn't need to be escaped. Just write
WHERE FIELD LIKE '%&%'
to search for entries that contain & somewhere in the field
Be aware though, that this will result in a full table scan as the server can't use any indexes. Had you typed WHERE FIELD LIKE '&%' the server could do a range seek to find all entries starting with &.
If you have a lot of data and can't add any more constraints, you should consider using SQL Server's full-text search to create and use and FTS index, with predicates like CONTAINS or FREETEXT
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 '!';
Sorry for my English
Text columns in my database contain English and not-English letters.
So I cannot retrieve (using LIKE) rows whose Title column meets (case-insentive) some pattern:
SELECT * FROM TableName WHERE TitleColumn LIKE '%pattern%';
or even:
SELECT * FROM TableName WHERE UPPER(TitleColumn) LIKE UPPER('%pattern%');
For example. If field Title contains "ГазПром", and pattern is "газ" (Г and г are the same letter in different cases), then neither of the two SELECT statements above cannot recognize a match.
What can help me?
Usually when you want to compare something case-insensitive, or you accent insensitive (i.e. á vs. a), you specify the collation using COLLATE keyword:
SELECT * FROM TableName WHERE TitleColumn LIKE '%pattern%' COLLATE 'German_CI_AI_SC';
Unfortunately, given its documentation I don't think SQLite supports COLLATE in selection, and at the same time it does not seem to support language-specific collations.
Please keep in mind, that case-insensitive comparisons require you to specify the language, as it case conversion rules differ between various languages...