I need to query tables in ServiceNow via its REST API while using multiple conditions grouped as following:
( (Condition1 AND Condition2) OR (Condition3 and Condition4) ) AND Condition5 AND Condition6
Does anyone know if this is possible, and if so, how? I've looked into the documentation but I'm not able to understand if it explains how to solve my problem.
Edit 1: I forgot to mention that I did try using parenthesis in my REST calls but it has not worked.
Thanks,
The only way I've figured out how to do it is using the ^NQ (New Query) operator.
In SQL the query would look like this:
WHERE [state] IN (16,17) AND ([assigned_to] = 'value1' OR [assignment_group] IN ('x','y','z'))
So my query looks like this:
sysparm_query=stateIN16,17^assigned_to=value1^NQstateIN16,17^assignment_groupINx,y,z
So you're actually duplicating the common filter (state) and using different filters on each side of the ^NQ.
I guess you could say it would be the equivalent of a UNION in SQL.
SELECT * FROM [wm_task] WHERE [state] IN (16,17) AND [assigned_to] = 'value1'
UNION
SELECT * FROM [wm_task] WHERE [state] IN (16,17) AND [assignment_group] IN ('x','y','z')
Related
Building a basic search tool in PostgreSQL.
When I setup a basic query like below I receive data.
SELECT *
FROM schema_name.table_name
WHERE column_name1 = 'sometext' AND column_name2 LIKE '%sometext%'
;
But when I and more conditions to the where clause like below, the query does execute, but I receive no data.
SELECT *
FROM schema_name.table_name
WHERE column_name1 = 'sometext' AND column_name2 LIKE '%sometext%' AND column_name3 = 'somemoretext'
;
I've tested individual queries to ensure there are records that meet all of the conditions and they exist. However I cannot find a way to tie the criteria together into one query. Most examples I've seen online stop at two conditions or do not combine 'AND' with 'LIKE'. Any help would be appreciated, thanks!
-Edit 1 Begins Here-
Sample Query that generates no data in PostgreSQL
SELECT *
FROM ctgov.studies
WHERE (overall_status = 'Recruiting') AND (official_title LIKE '%immunotherapy%') AND (source LIKE '%university%')
;
When I execute the same search via the website I gather data for the DB it returns 163 matching records. Using 'OR' in this scenario would retrieve records that won't match all the criteria I'm looking for.
I think you need OR instead AND
SELECT *
FROM schema_name.table_name
WHERE column_name1 = 'sometext' OR column_name2 LIKE '%sometext%' OR column_name3 = 'somemoretext'
Thanks for the help everyone, learned that my queries were too strict and I did not account for capitalization in text recognition.
I'm trying to write a SP that will allow users to search on multiple name strings, but supports LIKE functionality. For example, the user's input might be a string 'Scorsese, Kaurismaki, Tarkovsky'. I use a split function to turn that string into a table var, with one column, as follows:
part
------
Scorsese
Kaurismaki
Tarkovsky
Then, normally I would return any values from my table matching any of these values in my table var, with an IN statement:
select * from myTable where lastName IN (select * from #myTableVar)
However, this only returns exact matches, and I need to return partial matches. I'm looking for something like this, but that would actually compile:
select * from myTable where CONTAINS(lastName, select * from #myTableVar)
I've found other questions where it's made clear that you can't combine LIKE and IN, and it's recommended to use CONTAINS. My specific question is, is it possible to combine CONTAINS with a table list of values, as above? If so, what would that syntax look like? If not, any other workarounds to achieve my goal?
I'm using SQL Server 2016, if it makes any difference.
You can use EXISTS
SELECT * FROM myTable M
WHERE
EXISTS( SELECT * FROM #myTableVar V WHERE M.lastName like '%'+ V.part +'%' )
Can your parser built the entire statement? Will that get you what you want?
select *
from myTable
where CONTAINS
(lastName,
'"Scorsese" OR "Kaurismaki" OR "Tarkovsky"'
)
This can be done using CHARINDEX function combined with EXISTS:
select *
from myTable mt
where exists(select 1 from #myTableVar
where charindex(mt.lastName, part) > 0
or charindex(part, mt.lastName) > 0)
You might want to omit one of the conditions in the inner query, but I think this is what you want.
I am in a situation where I need to return results if some conditions on the string/character are met.
For example: to return only the names that contain 'F' character from the Person table.
How to create an SQL query based on such conditions? Is there any link to a documentation that explains how can SQL perform such queries?
Thanks in advance
The most basic approach is to use LIKE operator:
-- name starts with 'F'
SELECT * FROM person WHERE name LIKE 'F%'
-- name contains 'F'
SELECT * FROM person WHERE name LIKE '%F%'
(% is a wildcard)
Most RDBMS offer string operations which are able to perform that required task in one way or the other.
In MySQL you might use INSTR:
SELECT *
FROM yourtable
WHERE INSTR(Person, 'F') > 0;
In Oracle, this can be done, too.
In PostgreSQL, you can use STRPOS:
SELECT *
FROM yourtable
WHERE strpos(Person, 'F') > 0;
Usually there are several approaches to solve this, many would choose the LIKE operator. For more details, please refer to the documentation of the RDBMS of your choice.
Update
As requested by the questioner a few words about the LIKE operator, which are used not only in MySQL or Oracle, but in other RDBMS, too.
The use of LIKE will in some cases make your RDBMS try to use an index, it usually does not not try to do so if you use a string functions.
Example:
SELECT *
FROM yourtable
WHERE Person LIKE 'F%';
The query may look like this:
SELECT * FROM Person WHERE FirstName LIKE '%F%' OR LastName LIKE '%F%'
Writing a stored procedure that will have multiple input parameters. The parameters may not always have values and could be empty. But since the possibility exists that each parameter may contain values I have to include the criterion that utilizing those parameters in the query.
My query looks something like this:
SELECT DISTINCT COUNT(*) AS SRM
FROM table p
WHERE p.gender IN (SELECT * FROM Fn_SplitParms(#gender)) AND
p.ethnicity IN (SELECT * FROM Fn_SplitParms(#race)) AND
p.marital_status IN (SELECT * FROM Fn_SplitParms(#maritalstatus))
So my problem is if #gender is empty(' ') the query will return data where gender field is empty when I really want to just ignore p.gender all together. I don't want to have to accomplish this task using IF/ELSE conditional statements because they would be too numerous.
Is there any way to use CASE with IN for this scenario? OR
Is there other logic that I'm just not comprehending that will solve this?
Having trouble finding something that works well...
Thanks!
Use or:
SELECT DISTINCT COUNT(*) AS SRM
FROM table p
WHERE
(p.gender IN (SELECT * FROM Fn_SplitParms(#gender)) OR #gender = '')
AND (p.ethnicity IN (SELECT * FROM Fn_SplitParms(#race)) OR #race = '')
AND (p.marital_status IN (SELECT * FROM Fn_SplitParms(#maritalstatus)) OR #maritalstatus = '')
You might also want to consider table-valued parameters (if using SQL Server 2008 and up) - these can sometimes make the code simpler, since they are treated as tables (which in your case, may be empty) and you can join - plus no awkward split function required.
I was wondering if something like this was possible:
SELECT name or alt_name FROM users Where userid=somenum
I have also tried this:
SELECT name IF NOT NULL ELSE alt_name ...
If anyone has any insight I'd appreciate it.
If your trying to replace nulls then
select coalesce(name,alt_name)....
it will return first non null value
How about this?
SELECT IsNull(name, alt_name) FROM users Where userid=somenum
Its similar to the Coalesce function, but it only takes two arguments and is easier to spell.
Do you mean something like this? I'm assuming TSQL
SELECT CASE WHEN [name] IS NOT NULL THEN [name] ELSE [alt_name] END AS [UserName]
FROM [Users] WHERE [UserId] = somenum
Not the most efficient answer, but what about this:
SELECT name FROM users WHERE userid=somenum AND name IS NOT NULL
UNION
SELECT altname FROM users WHERE userid=somenum AND name IS NULL
I think this will produce what you are after.
SELECT CASE
WHEN userid = somenum THEN name
ELSE alt_name
END
FROM users
If you're using a database that supports them then one of the simplest ways is to use a user defined function, you'd then have
SELECT udfPreferredName() FROM users
where udfPreferredName() would encapsulate the logic required to choose between the name and alternative_name fields.
One of the advantages of using a function is that you can abstract away the choice logic and apply it in multiple SQL statements wherever you need it. Doing the logic inline using a case is fine, but will usually be (much) harder to maintain across a system. In most RDBMS the additional overhead of the function call will not be significant unless you are handling very large tables