Array value Passing into SQL Server IN Query - javascript

I am using Nodejs Typescript and SQL Server.
I wrote query to find array matching values from database. If I pass static array values then data is fetching. When add dynamic array values query is not executing.
My query is below.
SELECT Name,id,date FROM venue where name IN('Bangalore','Delhi')
below query is not executing
let input = ["Bangalore","Delhi"]
SELECT Name,id,date FROM venue where name IN('${input}')
What I observed is When I pass dynamic values into query its taking "" quotes ie "Bangalore","Delhi" But query is expecting is single quotes ''
I know In Nodejs we can pass ? and values as parameter but in our case we using typescript and sequelize it has limitation. I unable to use that format.

You need to use array#map to create quoted city name which will be passed inside IN clause.
const input = ["Bangalore","Delhi"],
query = `SELECT Name,id,date FROM venue where name IN (${input.map(city => `'${city}'`)})`;
console.log(query);

May be the values inside IN clause is passing as an array. Please take a look at this array value passing inside IN clause

Related

Axios get one parameter with multiple values separated by comma

I have a table in React with data about tests which I get from API. I have to do filters on frontend and be able to join them, for example filter tests from chosen category, chosen difficulty and created before some data.
If I wanted to filter for example tests from category "Javascript" and difficulty "Junior", I should get the uri:
/api/admin/filters?filter=category:Javascript,difficulty:JUNIOR
If I wanted to filter tests for "Junior" or "Mid" I should get:
/api/admin/filters?filter=difficulty:JUNIOR,difficulty:'MID
Note apostrophe here which stands for "or".
I should also be able to filter by creation date, for example:
/api/admin/filters?filter=creationDate<2019-09-23 17:34:21,creationDate>2019-09-12 17:34:21
I wonder how I can create such queries? URLSearchParams or axios params adds parameters separated by & so I can't use it here because I have one parameter "filter" with multiple values. Or maybe I should use it and also use js replace method for replacing & for comma? I have also no idea how to add apostrophe.
I saw similar question here: https://spectrum.chat/react/general/query-string-sending-multiple-value-to-the-same-parameter~5d029da0-e7da-443d-a4b2-1529ca7b4f82
but I can't use an array in my case because I have multiple filters. I suppose I have to create object like:
options = {
difficulty: [junior, mid],
category: [javascript],
created before: 2019-09-23 17:34:21,
created after: 2019-09-12 17:34:21
}
and now how to add keys and values from such object to uri so it looks like backend expects?
Any help would be appreciated.
Encoding parameters with encodeURI before passing to axios might solve your issue.
If you need to pass parameters with special characters (like '[',']' ...etc) you should give the parameter as a string '["junior","mid"]' instead of giving parameter as an array.
(Giving as an array will just remove the brackets)
var params = '["junior","mid"]'
encodeURI(params) // it returns "%5B%22junior%22,%22mid%22%5D"
var params = ["junior","mid"]
encodeURI(params) // it returns "junior,mid"

Sql query for data stored as JSON

I have data stored in column as JSON, as shown below:
{"category":{"value":CAT, "demo":A.......
I want to query in SSMS and want an output like:
CAT
I tried "SELECT JSON_QUERY(TABLENAME,'$.CATEGORY') FROM TABLENAME" which gave result of the
{"value":CAT, "demo":A....... but I want only CAT. How do I do that?
Use JSON_VALUE to get specific values.
SELECT JSON_VALUE(TABLENAME,'$.CATEGORY.VALUE') AS Value
JSON_QUERY is for JSON fragments.
Source
You should use JSON_VALUE.
SELECT JSON_VALUE(ColumnName, '$.category.value') FROM TableName
Take note of the first parameter, it should be the column name, not the table name (as opposed to your example).

Node.js Firestore Query Select List of field paths

I try to create and returns a new Query instance that applies a field mask to the result and returns only the specified subset of fields.
When i use :
let query = firestore.collection('col').select('field1','field2','field3').get() ...
it's ok, the query returns all the collection documents with only the 3 specified fields.
In my context application, the specified fields list is on a configuration document. When i use :
let fieldsList = ['field1','field2','field3'];
let query = firestore.collection('col').select(fieldsList).get() ...
i have an error message "Argument at index 0 is not a valid FieldPath ..."
On the Google documentation, it is specified "You can specify a list of field paths to return"
So, i don't know how to pass a list of field paths to the query select method.
Many thanks for your help !!!
You are working on what is known as spread syntax.
To make it work, it need to add triple dots in front of fieldList:
let query = firestore.collection('col').select(...fieldsList).get() ..

underscore .find() multiples with same key value

I am currently trying to filter an object via POST from a form the form is made of checkboxes and as a result I need to be able to search an array of objects for multiple values on the same key.
My POST looks similar to this,
{
tax_year: ['2016/17', '2017/18'],
status : ['completed'],
user : [1,4,78]
}
How would I go about search an array of objects and returning all the objects that have a matching key and value? I know I can do a single find with underscore like so,
var result = _.where(historyData, {tax_year: "2016/17"});
but I have no clue as to how to search for multiple matching keys and values?
It seems that you're matching on both of the values in an array value (regardless of whether there are more array values or not). If that's true, you might have to use the _.filter() method, where you can provide whatever logic you need in the 'predicate'. Alternatively, you could add a unique identifier on the server side to each record and match on that.

Query String generated by $.param() include square brackets for array

I have an object like this:
var queryObject= {
name: 'Shwetanka',
subjects: ['Mathematics', 'Physics', 'Computers'],
stream: 'science'
};
When I create query string with this using $.param(queryObject) I get this as query string:
name=Shwetanka&subjects%5B%5D=Mathematics&subjects%5B%5D=Physics&subjects%5B%5D=Computers&stream=science
Expected: name=Shwetanka&subjects=Mathematics&subjects=Physics&subjects=Computers&stream=science
How do avoid [] added by the method in the query string for params with same name. In the backend I'm using struts2 to read params.
I've found the solution. I just have to pass 'traditional=true' in $.param(queryObject, true). This generates the query string i want.
When we have fields with same name in a form and it is submitted via GET/POST, it is bound to be send as an array of params with the same name.
And the server would be expecting the values as such. So, Even if you somehow remove that [], it ceases to be an array, and the server will get only one value.
jQuery param method is designed, with the same thing in mind.
Check out examples in http://api.jquery.com/jQuery.param/.

Categories

Resources