How to remove quote marks from variable in Azure Data Factory? - azure-data-factory

I have variable called Query in Azure Data Factory.
I expect value like: Select CustomerName From Customer Where City = 'London'
However I occasionally get value like: 'Select CustomerName From Customer Where City = 'London''
I would like to remove single quote from beginning and end if value starts with ' Quote should not be removed if value does not start with quote.
How to remove quote marks from variable?

Use the If() function in the expression to evaluate if the variable value starts with a single quote (').
If the expression returns true then using substring exclude the first and last single quotes from the variable value. If false the value returns with no change.
Example:
Here, the variable (Query) value contains a single quote at the start and end of the value.
Using the Set variable activity, store the result in a variable (Final_query) after removing quotes if it contains at the start of the value.
#if(equals(substring(variables('Query'),0,1),''''),substring(variables('Query'),1,sub(length(variables('Query')),2)),variables('Query'))
Output:
Removed single quotes from start and end of the value.

Related

#{item().TableName} only gives first part of string

I have database from which I take nvarchar to lookup. Then I transport it to foreach. My problem is that when I use #item().TableName in dataset properties as value it works fine but when I use #{item().TableName} in query it takes only first part of it. So when it is "Receipt Header" I only get "Receipt"
Enclose the table name within the square brackets '[]' if it contains space or any special characters and use concat() function when combining constants with variables in the expression.
Query:
#concat('select MAX([Creation Date]) as NewWatermarkvalue from ',item().sourceSchema,'.[',item().TableName),']')

Using a variable in the Request Body form in Azure Data Factory Copy Data activity

Is it possible to use a variable in the "Request body" field in a Copy data activity?
This is my pipeline:
Output of "Get requestBody":
Allready full of unwanted slashes.
Output of "Set variable":
I using this expression in the "Request body" Field:
#replace(variables('requestBody'),'\','')
This is the input of "Copy data":
I cant get rid of all slashes. Is it even possible to use a variable for "Request body"?
As your lookup activity output returns JSON string value it includes an escape character backslash '\'.
To remove the escape character, create an array type variable and convert the lookup out to JSON array in append variable activity as below.
Create array variable.
Lookup output: As I checked firstrow only property in lookup activity, the output results firstrow only.
In the Append variable activity, get the lookup output and convert it to JSON array.
#json(activity('Lookup1').output.firstrow.requestbody)
Append variable result: Use this variable in later activities.
You can refer to these SO & SO threads for reference.
To remove the backslashes, I had to use this:
#replace(string(variables('requestBody')), '\"', '"')

How to add bash array variable to mongo update query

I am writing a bash script which updates a mongo document. If it's a new document then it creates a new document in mongo else updates it.
I want to pass bash array variable to the mongo query since few fields are of array type. I am not sure how to pass the array field. Any help would be appreciated.
This is my query:
db.my_col.update({emp_id: '"'$emp_id'"'}, {$set: {contacts: '"'${contacts}'"', emp_name: '"'$emp_name'"'}}, {upsert: true})
If I just add like normal variable the only first value in variable gets added.
If you put your array variable into a string, only the first item will be there, right
Basically, you get all of them with this syntax ${contacts[#]}, but they will be unquoted and separated with spaces and likely won't work in a query because of wrong syntax.
So you'd need to manually convert a Bash array to a JS array. I don't know an automagical way to do it, but consider this function:
function jsarray {
res=""
for a in ${#}; do
res="$res,\"$a\""
done
echo [ ${res:1} ]
}
It goes through it's arguments, wraps each one in quotes and joins together with commas.
Use it like this (I assume $contacts is an array of string values):
IFS=""
contacts=("skype name" "email address" phone website whatever)
jscontacts=$(jsarray $contacts[#])
# Proceed to using $jscontacts in your query.
After that $jscontacts will be a string value of [ "skype name","email address","phone","website","whatever" ].
Notes:
IFS="" - IFS stands for internal field separator, this is how Bash separates arguments from each other. By default it's " " (a space). Resetting it to nothing to preserve array items with spaces in them; otherwise "skype name" beсome two separate values of "skype" & "name".
${res:1} - skips the first character from $res (because it's a comma).
jscontacts=$(...) captures the terminal output into a jscontacts variable.

pg_get_serial_sequence in postgres fails and returns misleading error

This is not obviuos to me.
When I do:
SELECT MAX("SequenceNumber") FROM "TrackingEvent";
It returns perfectly fine with the correct result
When I do:
SELECT nextval(pg_get_serial_sequence("TrackingEvent", "SequenceNumber")) AS NextId
It returns an error which says
column "TrackingEvent" does not exist.
Not only is it wrong but the first argument of the function pg_get_serial_sequence takes a table name and not a column name, so the error is aslo misleading.
Anyways, can someone explain to me why I get an error on the pg_get_serial_sequence function ?
pg_get_serial_sequence() expects a string as its argument, not an identifier. String constants are written with single quotes in SQL, "TrackingEvent" is an identifier, 'TrackingEvent' is a string constant.
But because the function converts the string constant to an identifier, you need to include the double quotes as part of the string constant. This however only applies to the table name, not the column name, as explained in the manual
Because the first parameter is potentially a schema and table, it is not treated as a double-quoted identifier, meaning it is lower cased by default, while the second parameter, being just a column name, is treated as double-quoted and has its case preserved.
So you need to use:
SELECT nextval(pg_get_serial_sequence('"TrackingEvent"', 'SequenceNumber'))
This is another good example why using quoted identifiers is a bad idea. You should rename "TrackingEvent" to tracking_event and "SequenceNumber" to sequence_number

Escape character in JPQL

In JPQL what is escape character we can use to escape characters such as "'"?
Ex : I am doing something like
...where person.name='Andy'
Here it is working fine
but when the person's name is Andy's then the where clause becomes like
...where person.name='Andy's'
and it returns an error saying
It cannot figure out where string literal ends. Solution is nicely told in specification:
A string literal that includes a single quote is represented by two
single quotes—for example: ‘literal’’s’.
In your case means:
...where person.name='Andy''s'
Below is the sample code for executing query using named parameter.
Query query = entityManager.createQuery("SELECT p FROM Person p WHERE p.name LIKE :name" );
query.setParameter("name", personName);
Here, you can pass string to personName which may contain special character like "Andy's".
Also, it looks much clean & doesn't require to check parameter before query execution & altering the search string.