SELECT string expression recognizes single not double quotes? - azure

I'm creating an Azure Stream Analytics query and I needed to output a constant column header/value, and I noticed that if I include a string expression as a SELECT item, it needs to be enclosed in single (') not double (") quotes, otherwise I get a NULL.
SELECT 'foo' as "bar" INTO ... FROM ... >>> outputs foo as a value of bar.
SELECT "foo" as "bar" INTO ... FROM ... >>> outputs null as a value of bar.
Why does a string literal require single quotes? And if I use double quotes, what is it interpreting that literal as?
Thanks
-John

Based on my test, both single (') and double (") quotes could get the result.
My test json as below:
{"plantId": "Plant A", "machineId" : "M001", "sensorId": "S001", "unit": "kg", "time": "2017-09-05T22:00:14.9410000Z", "value": 1234.56}
{"plantId": "Plant A", "machineId" : "M001", "sensorId": "S001", "unit": "kg", "time": "2017-09-05T22:00:19.5410000Z", "value": 1334.76}
The result as below:
If you use single quote to select the value, the result will be the single quote's value. The query will not regard the value as the column name to select the result:
Like this:
But if you use double quote to select the value, the query will regards it as the column name. So you will get the null result. Because the query couldn't select the data.

Related

How to maintain quotes while exploding json in spark-sql

I have a column in string format like below:
["name": "XXX","active": true,"locale": "EN","Channel":["1","2"]]
I would like to explode them like below in spark sql(preserving the quotes in string values).
This is code I used:
SELECT EXPLODE(from_json(col, 'map<string, string>>'))
FROM XXX;
I am not able to preserve the quotes in "XXX" and "EN" after exploding.
This is what I want:
key
value
name
"XXX"
active
true
locale
"EN"
Channel
[1,2]
The quotes are part of the JSON representation of the data and not the data itself. If there were embedded quotes in the data it would look like:
"\"SOME DATA\""
If you need to add quotes on strings, you can always concatenate them to the specific columns. You can use the concat operator to accomplish this, https://spark.apache.org/docs/latest/api/sql/index.html#concat
Alternatively, you can use get_json_object, which allows you to extract specific parts of a JSON object. https://spark.apache.org/docs/3.1.2/api/python/reference/api/pyspark.sql.functions.get_json_object.html

How to replace a single quote with double quotes in ADF dynamic expressions

I am trying to replace single quote in a string with double quote using replace function with data factory expressions.
For example, replace single quote in the following string
hello'world ---> hello''world
#replace(pipeline().parameters.tst,''','''')
The above code is not working. Need help in fixing the code
You can declare a new parameter with the value ' (single quote). You can look at the following demonstration for reference.
I have taken 2 parameters, text with the value hello'world and replace_char with the value '.
I used a set variable activity to store the output of the replace() function (for demonstration) into variable named output (String). Now, I modified the value as:
#replace(pipeline().parameters.text,pipeline().parameters.replace_char,'"')
This successfully helps in replacing a single quote with double quote character.
NOTE: The \ in the output variable value indicates that the " is to be considered as a character inside the string value.
Use two single quotes to escape a ' character in string functions.
For example, expression #concat('Baba', '''s ', 'book store') will return below result.
Baba's book store
https://learn.microsoft.com/en-us/azure/data-factory/control-flow-expression-language-functions#escaping-single-quote-character

Handling special characters in ARRAY_CONTAINS search in cosmos sql query

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

pythonstring value vs string in django orm

this is what I want
I made the parameter by str value.
because I have to get parameters by list variable.
but when I use str parameter in filter, wrong result is comming.
whole source is here.
In the first picture, you are providing a list of strings and in the second picture, you are providing string.
You can solve it by:
import json
fieldQueryString = json.loads(fieldQueryString)
this will convert this string into a list. So the output will change from
'["001", "002", "004", "005", "006"]' # this is a string
to
["001", "002", "004", "005", "006"] # this is a list
(notice the quotes before and after [ and ]).

How to escape singlequote(') in azure logic app expression replace function

In Azure logic Apps, how can I escape single quotes(') using a replace function?
I have a JSON payload where I have to replace a single quote(') with a double quote(").
The expression I've came up with looks like this:
replace(string(#triggerBody()),'/' ','/" ')
But my second expression to escape the single quote (') isn't working.
I resolved this by using a double single quotation, '' thanks to this link
Summary for single and doubt quotes sharing with others.
String containing single quote ', use extra single quote to escape:
''
String containing double quotes ", prefix with a forward slash to escape:
\"
Original json (posted from postman):
{
"name": "single''dds double\"te",
"email": "special signle and double quotes",
"password": "pp#pp"
}
console.log result in sql query in Nodejs environment:
INSERT INTO ztestTbl(name, email, passowrd) VALUES (N'single''dds double"te', N'special signle and double quotes', N'pp#pp')
String insert into the mssql db:
single'dds double"te
This is now you do it. You need 2 single quotes inside the single quote
#replace(string(triggerBody()),'''' ','\" ')
Try this:
#replace(string(triggerBody()),''' ','\" ')
HTH
I had problem escaping single quote in query parameter concat. I resolved it by used double quote. Thanks to tips in this post.
concat('**Text1**', ''**'**' , '**text2**',''**'**')
resulted in :
Text1'text2'
Take a note of four single quotes.

Resources