How can I search with special characters? - sql

I'm manager for a project from Vietnam search in SQL. Some thing like this
select N'Trên bootrap , click nút setting ko thấy phản ứng' as title into #test
select * from #test where title like N'%Trên%'
select * from #test where title like N'%ứng%'
-------------------
select * from #test where title like N'%ứng%'
But today my customer give me some characters like above.
From sql select above you can see:
Trên = Trên => it Ok. But
ứng <> ứng (because user input from orther computer). I don't know how can solove this situation.

You can try this
SELECT * FROM #test WHERE title LIKE "%ứng%" COLLATE utf8mb4_german2_ci
utf8mb4_unicode_ci would probably work as well.

Related

Simulating an if condition in the WHERE clause of an SQL query

I am trying to write a google BigQuery to select some records based on this condition:
if ID_parameter is not null:
SELECT * WHERE ID=ID_parameter
else:
SELECT * WHERE country_id=country_id_parameter and (name LIKE %name_parameter% OR name is null)
The name_parameter should be optional but one of the ID_parameter or country_id_parameter must be provided.
Sorry, I am a newbie with SQL but I hope I stated what I want clearly.
consider below approach
select *
from your_table
where case
when not id_parameter is null then id = id_parameter
else country_id = country_id_parameter and (regexp_contains(name, name_parameter) or name is null)
end
I miss the table name in the query, let's assume it's 'personnel'.
Suppose ID-parameter = 200, country_id_parameter = 'NL', name_parameter = '%bart%'
Maybe like this when the name is 'bart':
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL' and
name LIKE '%bart%')
Use this when the name is NULL:
select * from personnel
where ID=200
or (ID=NULL and
country_id='NL')

Is it possible to select more statements in SQLite?

I have the following code :
SELECT Description FROM KeysDB WHERE Count='0 (Blocked)' OR ErrorCode='0xC004C060' AND Description LIKE '%Win%'
It works normally until I change it.
SELECT Description FROM KeysDB WHERE Count='0 (Blocked)' OR ErrorCode='0xC004C003' OR ErrorCode='0xC004C060' AND Description LIKE '%Win%'
I want to select all Windows product key with Count = 0 (Blocked) or ErrorCode = 0xC004C060 or ErrorCode = 0xC004C003
and / or have different operator precedence. That means their binding strength is differently.
You can use parentheses to make it how you like. Not sure if this is how you intended it but you get the picture:
SELECT Description
FROM KeysDB
WHERE
(
Count='0 (Blocked)' OR ErrorCode='0xC004C003' OR ErrorCode='0xC004C060'
)
AND Description LIKE '%Win%'

Apply like function on an array is SQL Server

I am getting array from front end to perform filters according that inside the SQL query.
I want to apply a LIKE filter on the array. How to add an array inside LIKE function?
I am using Angular with Html as front end and Node as back end.
Array being passed in from the front end:
[ "Sports", "Life", "Relationship", ...]
SQL query is :
SELECT *
FROM Skills
WHERE Description LIKE ('%Sports%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Life%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Relationship%')
But I am getting an array from the front end - how to create a query for this?
In SQL Server 2017 you can use OPENJSON to consume the JSON string as-is:
SELECT *
FROM skills
WHERE EXISTS (
SELECT 1
FROM OPENJSON('["Sports", "Life", "Relationship"]', '$') AS j
WHERE skills.description LIKE '%' + j.value + '%'
)
Demo on db<>fiddle
As an example, for SQL Server 2016+ and STRING_SPLIT():
DECLARE #Str NVARCHAR(100) = N'mast;mode'
SELECT name FROM sys.databases sd
INNER JOIN STRING_SPLIT(#Str, N';') val ON sd.name LIKE N'%' + val.value + N'%'
-- returns:
name
------
master
model
Worth to mention that input data to be strictly controlled, since such way can lead to SQL Injection attack
As the alternative and more safer and simpler approach: SQL can be generated on an app side this way:
Select * from Skills
WHERE (
Description Like '%Sports%'
OR Description Like '%Life%'
OR Description Like '%Life%'
)
A simple map()-call on the words array will allow you to generate the corresponding queries, which you can then execute (with or without joining them first into a single string).
Demo:
var words = ["Sports", "Life", "Relationship"];
var template = "Select * From Skills Where Description Like ('%{0}%')";
var queries = words.map(word => template.replace('{0}', word));
var combinedQuery = queries.join("\r\n");
console.log(queries);
console.log(combinedQuery);

Appending in middle of text in column data oracle sql

I need to append the text in middle of text .
1 EX :existing text is : "Monoclonal Anti-FLAG, Clone 6F7"
Result :"<HIDE>Monoclonal Anti-<HIDE>FLAG, Clone 6F7".
2 EX :existing text is : "Anti-100-KD subunit"
Result : "<HIDE> Anti-</HIDE>100-KD subunit"
I want to append <HIDE> tag where ever Anti and Monoclonal term is coming.
select * from table where regexp_like(colnanme, '^(Monoclonal Anti|Anti)')
Can you suggest how should i write this .for select i have written query .
The predicted certain regular expression solution.
-- Start test data
with test_data as
(select 'Monoclonal Anti-FLAG, Clone 6F7' as test_string from dual union all
select 'Anti-100-KD subunit' from dual)
-- End test data
select test_string,
REGEXP_REPLACE(test_string,'(Monoclonal Anti-|Anti-)','<HIDE>\1</HIDE>')
from test_data;
"TEST_STRING" "Result"
"Monoclonal Anti-FLAG, Clone 6F7" "<HIDE>Monoclonal Anti-</HIDE>FLAG, Clone 6F7"
"Anti-100-KD subunit" "<HIDE>Anti-</HIDE>100-KD subunit"
you could use replace and like
select replace(colname, 'Anti', '"<HIDE> Anti-</HIDE> )
from table where colname like '%Anti%'
and colname not like 'Monoclonal Anti%';
of for a combined situation you can use a case when for lead replace
select case when colname like '%Monoclonal Anti%'
then replace(colname, 'Monoclonal Anti', '"<HIDE> Monoclonal Anti-</HIDE> )
when colname like '%Anti%'
then replace(colname, 'Anti', '"<HIDE> Anti-</HIDE> )
else colname
end
from table where colname like '%Anti%';

SQL Server can not find word

When I search for the Dünyas word with a LIKE operator I can see the results but when I search Dünyası word with LIKE operator, it returns no results. Problem is not ı or ü letters I tried with i and u letters as well.
This query:
select * from DbDivx where NameTurkish like '%Dünyas%' =>
returns:
Boksörün Dünyası
Geleceğin Dünyası
Su Dünyası
However:
select * from DbDivx where NameTurkish like '%Dünyası%' =>
returns nothing.
Can you help me please?
http://sqlfiddle.com/#!6/8c359/1
create table test (someName nvarchar(300))
insert into test (someName)
select N'Boksörün Dünyası'
union all select N'Geleceğin Dünyası'
union all select N'Su Dünyası'
select * from test where someName like N'%Dünyası%'
Not having any issues with the above fiddle. Note that when i inserted data without the N in front of it, the like query did not return results.
hopefully this helps!