Handling special characters in ARRAY_CONTAINS search in cosmos sql query - azure

I have db structure like this :
"selfId": "cd29433e-f36b-1410-851b-009d805073d7",
"selfName" : "A",
"bookIds": [
"2f51bfd4",
"2f3a1010",
"090436c0",
"1078c3b2",
"b63b06e0"
]
I am working in C# and get bookId as a string.
I am writing query as :
string SQLquery = string.Format("select c.selfName from c where ARRAY_CONTAINS(c.bookIds,\""+bookId+"\"");
But When bookId contains special character, then query is giving error. For example if bookId = "AK"s" book" (please note id itself contains ") , then executing sqlQuery is giving error.

When a text contains quotes, slashes, special characters, you can use Escape Sequence
The quoted property operator [""] can also be used to access properties. for example, SELECT food.id and SELECT food["id"] are equal. This syntax can be used to escape a property with spaces, special characters, or a name that is the same as a SQL keyword or reserved term.
Example:
SELECT food["id"]
FROM food
WHERE food["foodGroup"] = "Snacks" and food["id"] = "19015"
Reference :- https://learn.microsoft.com/en-us/azure/cosmos-db/sql/sql-query-constants#bk_arguments

Related

nodejs how to replace ; with ',' to make an sql query

I have a query that looks like this:
INSERT INTO table VALUES ('47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3')
that is produced by parsing a csv file.
The query is not in a valid form, I have to replace all semicolons with the string ',' (comma inside single quotes). What I want to get is:
('47677','2019','2019','10T-1001-10010AS','A05','International','TieLineKoman-KosovoB','L_KOM-KOSB','2018','NULL','NULL','','NULL','Tieline','NULL','10XAL-KESH-----J','0','3')
I have tried to do this in many different ways, but I end up with backshlashes added in my string. This is what I get:
"INSERT INTO AllocatedEICDetail VALUES ('47677\\',\\'2019\\',\\'2019\\',\\'10T-1001-10010AS\\',\\'A05\\',\\'International\\',\\'TieLineKoman-KosovoB\\',\\'L_KOM-KOSB\\',\\'2018\\',\\'NULL\\',\\'NULL\\',\\'\\',\\'NULL\\',\\'Tieline\\',\\'NULL\\',\\'10XAL-KESH-----J\\',\\'0\\',\\'3')"
Any ideas how to do this properly without having the backslashes added?
Thank you!
//the string you have
const string = '47677;2019;2019;10T-1001-10010AS;A05;International;TieLineKoman-KosovoB;L_KOM-KOSB;2018;NULL;NULL;;NULL;Tieline;NULL;10XAL-KESH-----J;0;3';
//the string you need:
const targetString = string.replace(/\;/g,',');
You specify a small regex between the forward slashes in replace which is a simple ';', give it a 'g' flag for global which will replace all instances, and in the second argument supply what you need it replaced with.

Azure search using just special characters in search returns no results

we are using azure search API but are getting no results for special characters that contain no alphanumeric characters.
We was having trouble returned any matching results for Japanese language and any special characters at all until we wrapped the string in quotes (") see the examples below (we are escaping the special characters also.
strings that did not work
var searchTerm = "嘘つきな唇で";
var searchTerm = "test#123";
var searchTerm = "?sd-^&*d$£(";
After wrapping in quotes i.e.
searchTerm = "\"" + searchTerm + "\"*"
all the above searches returned the expected matches but now we have an issue of no matches with strings with only special characters in i.e.
var searchTerm = "####";
var searchTerm = "&#*(%$";
new SearchParameters
{
SearchFields = new List<string> {"name", "publicId"},
Top = 50,
SearchMode = SearchMode.Any,
QueryType = QueryType.Simple,
Filter = $"status eq 1"
}
Any help on this would be greatly appreciated
Kind regards
Without escaping or using another analyzer (rather than StandardAnalyzer which is the default) I don't think you'll be able to retrieve the results as some of the samples you've provided are reserved / special chars:
Please check:
https://learn.microsoft.com/en-us/azure/search/query-lucene-syntax#escaping-special-characters
EDIT: please read about analyzers in here: https://learn.microsoft.com/en-us/azure/search/search-analyzers

How to fix 'Unclosed quotation mark after the character string \')\'.' error

I'm generating a dynamic sql query based on some user input. Here is the code that prepares the query:
var preparedParamValues = paramValues.map(paramValue => `'${paramValue}'`).join(',');
var sql = `INSERT INTO [DB] (${paramNames}) VALUES (${preparedParamValues})`;
When I send the following string to the DB it throws the below error:
'They're forced to drive stupid cars.'
I get an error :
'Unclosed quotation mark after the character string \')\'.'
I'm trying to find a way to escape all those characters but I don't understand the error or at least the last part of it with all the symbols.
You have to use two single quotes when a single quote appears in the string:
'They''re forced to drive stupid cars.'

Display the specific part of the string in PostgreSQL 9.3

I have a string to modify as per the requirements.
For example:
The given string is:
str1 varchar = '123,456,789';
I want to show the string as:
'456,789'
Note: The first part (delimited) with comma, I want to remove from string and show the rest of string.
In SQL Server I used STUFF() function.
SELECT STUFF('123,456,789',1,4,'');
Result:
456,789
Question: Is there any string function in PostgreSQL 9.3 version to do the same job?
you can use regular expressions:
select substring('123,456,789' from ',(.*)$');
The comma matches the first comma found in the string. The part inside the brackets (.*) is returned from the function. The symbol $ means the end of the string.
A alternative solution without regular expressions:
select str, substring(str from position(',' in str)+1 for length(str)) from
(select '123,456,789'::text as str) as foo;
You could first turn the string to array and return second and third cell:
select array_to_string((regexp_split_to_array('123,456,789', ','))[2:3], ',')
Or you could use substring-function with regular expressions (pattern matching):
SELECT substring('123,456,789' from '[0-9]+,([0-9]+,[0-9]+)')
[0-9]+ means one or more digits
parentheses tell to return that part from the string
Both solutions work on your specific string.
Your The SQL Server example indicates you just want to remove the first 4 characters, which makes the rest of your question seem misleading because it completely ignores what's in the string. Only the positions matters.
Be that as it may, the simple and cheap way to cut off leading characters is with right():
SELECT right('123,456,789', -4);
SQL Fiddle.

How to search for unicode characters in records of DB2?

I have a table in DB2 say METAATTRIBUTE wherein a column say "content" might contain any special character including the unicode characters.
For any special character, Eg: "#" I can simply search by :
Select * from METAATTRIBUTE where content like '%#%';
but how to search for unicode characters like "u201B" or "u201E" ???
Thanks in advance.
Assuming you are talking about DB2 LUW, the Unicode string literals are designated by the symbols "u&", followed by a regular string literal in single quotes. Unicode code points are designated by an escape character, backslash by default. For example:
$ db2 "values u&'\201b'"
1
---
‛
1 record(s) selected.
So your query would look like:
Select * from METAATTRIBUTE where content like u&'%\201b%';
Recently, I have had the same problem. This worked for me
select *
from METAATTRIBUTE
where MEDEDELINGSZONE like '%' || UX'201B' || '%'

Resources