Grouping multiple 'AND x not like y' In SQL statement - sql

I currently have an SQL statement that is too long for my program (I have a maximum number of character that I can use. I'm using sccm report). The problem is my SQL statement look like this:
Select distinct v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0, v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0, v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername)
DisplayName0 NOT LIKE 'hpp%'
AND
DisplayName0 NOT LIKE 'Logitech SetPoint%'
AND
DisplayName0 NOT LIKE 'HP Document Manager%'
AND
DisplayName0 NOT LIKE 'HP Imaging Device Functions%'
AND
DisplayName0 NOT LIKE 'PyQt4 - PyQwt5%'
And it goes on and on for 20 pages. How can I minimize the amount of code this request contains? Is there a way to group all the displayName0 not like ?? with something like a NOT IN(value1, value2, ...)?

If you are OK without tailing % in you pattern you can replace it with:
SELECT ... WHERE DisplayName0 NOT IN ('hpp','Logitech SetPoint','HP Document Manager',...)
It would make it somehow shorter.
But it seems to me that proper solution would be to create [temp] table with all the names you need to filter against and then join it.

Could you store the values in a separate table and then reference it in your query like this?:
SELECT DISTINCT v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0
,v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0
,v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername) DisplayName0 NOT IN (
SELECT DisplayName0
FROM < NewTableName >
)

Related

How to query across multiple databases

I have a set of data that belongs to two different tables in two different databases. I have performed the query but I am getting a syntax error.
The two databases are:
dbo.Serve and dbo.Back
This is what I am doing:
SELECT Back.[dbo].Back$.Last_refreshed, dbo.Back.Backup_Name, dbo.Back$.Fle, dbo.Back$.Free_Space_TB, dbo.Backup$.Operational_status, dbo.Serve$.Name,
dbo.Back$.Rack_Enclosure, dbo.Back$.Server_Name, dbo.Back$.SIZE_TB, x.field, x.piv, dbo.Serve$.SKU,
CASE
WHEN dbo.Serve$.SKU like 'F1%' or dbo.Serve$.SKU like 'F2%' or dbo.Server_History$.SKU like 'F3%' or
dbo.Serve$.SKU like 'FY14%' or dbo.Serve$.SKU like 'F5%' or dbo.Server_History$.SKU like 'F6%' then 'Group1'
WHEN dbo.Serve$.SKU like 'Gr%' or dbo.Serve$.SKU like 'Fr%' or dbo.Server_History$.SKU like 'Ex%'
or dbo.Serve$.SKU like 'Ga%' or dbo.Serve$.SKU like 'Ho%' or dbo.Serve$.SKU like 'Gen%' then 'Group2'
END AS grp
FROM dbo.Back$
inner join dbo.Serve$ ON dbo.Serve$.Name = dbo.Back$.Server_Name and dbo.Back$.Last_refreshed = dbo.Serve$.Last_refreshed
cross apply (values (dbo.Back$.Free_Space_TB, 'Available'), (dbo.Back$.SIZE_TB - dbo.Back$.Available, 'Used')) AS x(field, piv)
Why would it give me an error on this part?
SELECT Back.[dbo].Back$.Last_refreshed,
as the syntax appears correct
Any help is appreciated.
This Back.[dbo].Back$.Last_refreshed,
does not make sense. You can still use databasename.schemaname.columnname
or a four part name (if you set up linked servers...
instancename.databasename.schemaname.columnname),
but it is not clear what you are referring to with: Back.[dbo].Back$.Last_refreshed

us WHERE statement and ARRAY for AthenaQuery

I have an SQL query like this (written for PostgreSQL):
SELECT *
FROM users
WHERE users.company_id = ANY(ARRAY[945387, 969109, 1460013, 1460044]) AND
users.profession_id = ANY(ARRAY[2738, 6388])
GROUP BY users.company_id
but I can't run it like this for AthenaQuery (getting SYNTAX_ERROR: Function any not registered).
How can I convert it?
You can rewrite the array operator to a standard IN condition.
Your query is equivalent to:
SELECT *
FROM users
WHERE users.company_id IN (945387, 969109, 1460013, 1460044)
AND users.profession_id IN (2738, 6388)
GROUP BY users.company_id

Apply like function on an array is SQL Server

I am getting array from front end to perform filters according that inside the SQL query.
I want to apply a LIKE filter on the array. How to add an array inside LIKE function?
I am using Angular with Html as front end and Node as back end.
Array being passed in from the front end:
[ "Sports", "Life", "Relationship", ...]
SQL query is :
SELECT *
FROM Skills
WHERE Description LIKE ('%Sports%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Life%')
SELECT *
FROM Skills
WHERE Description LIKE ('%Relationship%')
But I am getting an array from the front end - how to create a query for this?
In SQL Server 2017 you can use OPENJSON to consume the JSON string as-is:
SELECT *
FROM skills
WHERE EXISTS (
SELECT 1
FROM OPENJSON('["Sports", "Life", "Relationship"]', '$') AS j
WHERE skills.description LIKE '%' + j.value + '%'
)
Demo on db<>fiddle
As an example, for SQL Server 2016+ and STRING_SPLIT():
DECLARE #Str NVARCHAR(100) = N'mast;mode'
SELECT name FROM sys.databases sd
INNER JOIN STRING_SPLIT(#Str, N';') val ON sd.name LIKE N'%' + val.value + N'%'
-- returns:
name
------
master
model
Worth to mention that input data to be strictly controlled, since such way can lead to SQL Injection attack
As the alternative and more safer and simpler approach: SQL can be generated on an app side this way:
Select * from Skills
WHERE (
Description Like '%Sports%'
OR Description Like '%Life%'
OR Description Like '%Life%'
)
A simple map()-call on the words array will allow you to generate the corresponding queries, which you can then execute (with or without joining them first into a single string).
Demo:
var words = ["Sports", "Life", "Relationship"];
var template = "Select * From Skills Where Description Like ('%{0}%')";
var queries = words.map(word => template.replace('{0}', word));
var combinedQuery = queries.join("\r\n");
console.log(queries);
console.log(combinedQuery);

Using 'LIKE' in SQL Statement doesn't seem to work

I seem to be able to do a simple query that uses the equal sign, but am unable to find any results when I use the 'LIKE' function. Am I missing something here?
SELECT ISARPapers.*, AuthorList.FirstName, AuthorList.LastName
FROM AuthorList INNER JOIN (ISARPapers INNER JOIN PaperAuthor
ON ISARPapers.PaperID = PaperAuthor.PaperID)
ON AuthorList.AuthorID = PaperAuthor.AuthorID
WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
be carrefuly with capital letters
... WHERE ISARPapers.PaperTitle LIKE '%Mianzi%'
maybe Mianzi does not exists but mianzi does.
if you do not care about capitals, try:
... WHERE lower(ISARPapers.PaperTitle) LIKE '%mianzi%'

Why is this query not working for number 1?

this query returns 5 results as expected
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '0'
but this query returns nothing
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
the USERFIELD1 is a varchar 80 field
Here is all the data that is in the DB and as you can see there are two records with USERFIELD1 = '1' but there seems to be a line break after them...maybe that is causing the issue...is there anyway to grab those two records
LEADID,CREATEUSER,CREATEDATE,MODIFYUSER,MODIFYDATE,ACCOUNTMANAGERID,ASSIGNDATE,BUSINESSDESCRIPTION,COMPANY,COMPANY_UC,CREDITRATING,DATAQUALITY,DESCRIPTION,DIVISION,DONOTSOLICIT,EMAIL,EMPLOYEES,FAX,FIRSTNAME,HOMEPHONE,IMPORTID,IMPORTSOURCE,INDUSTRY,INTERESTS,ISPRIMARY,LASTCALLDATE,LASTNAME,LASTNAME_UC,LEADSOURCEID,MIDDLENAME,MOBILE,NEXTCALLDATE,NOTES,PREFERRED_CONTACT,PREFIX,PRIORITY,QUALIFICATION_CATEGORYID,REVENUE,SECCODEID,SICCODE,STATUS,SUFFIX,TICKER,TITLE,TOLLFREE,TYPE,USERFIELD1,USERFIELD2,USERFIELD3,USERFIELD4,USERFIELD5,USERFIELD6,USERFIELD7,USERFIELD8,USERFIELD9,USERFIELD10,WEBADDRESS,WORKPHONE,LEAD_ADDRESSID,DONOTEMAIL,DONOTFAX,DONOTMAIL,DONOTPHONE
Q134915558 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Harry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ134915558 ,NULL,NULL,NULL,NULL
Q39769667 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper2,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ39769667 ,NULL,NULL,NULL,NULL
Q488888476 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ488888476 ,NULL,NULL,NULL,NULL
Q504845720 ,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000S,2011-09-20 17:06:10.053,U6UJ9A00000G,2011-09-20 17:06:10.053,NULL,Rafner ext.,RAFNER EXT.,NULL,NULL,NULL,NULL,0,raf#raf.com,NULL,NULL,James,4075615519,NULL,NULL,sales,NULL,NULL,NULL,Rafner,RAFNER, ,NULL,NULL,NULL,Raf associates is asking a question,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,QQ504845720 ,NULL,NULL,NULL,NULL
Q539171226 ,U6UJ9A00000S,2011-09-20 17:49:28.963,U6UJ9A00000S,2011-09-20 17:49:28.963,NULL,2011-09-20 17:49:28.963,NULL,scaoo,SCAOO,NULL,NULL,NULL,NULL,0,harry333#harry.com,NULL,NULL,upper3,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is a download,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ539171226 ,NULL,NULL,NULL,NULL
Q547088411 ,U6UJ9A00000S,2011-09-20 17:46:18.103,U6UJ9A00000S,2011-09-20 17:46:18.103,NULL,2011-09-20 17:46:18.103,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,markus#gmail.com,NULL,NULL,upper,NULL,NULL,NULL,sales,posnation.com online demo request,T,NULL,Scott,SCOTT,L6UJ9A000004,NULL,NULL,NULL,this is from upper,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4074615519,QQ547088411 ,NULL,NULL,NULL,NULL
Q913526837 ,U6UJ9A00000S,2011-09-20 17:36:10.053,U6UJ9A00000S,2011-09-20 17:36:10.053,NULL,2011-09-20 17:36:10.053,NULL,Johndoe,JOHNDOE,NULL,NULL,NULL,NULL,0,test#gmail.com,NULL,NULL,Parry,NULL,NULL,NULL,NULL,Restaurant Pro Express demo download,T,NULL,Scott,SCOTT, ,NULL,NULL,NULL,this is from the site agan,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,1
,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,4075559999,QQ913526837 ,NULL,NULL,NULL,NULL
Q925684753 ,U6UJ9A00000S,2011-09-21 09:36:10.420,U6UJ9A00000S,2011-09-21 09:36:10.420,NULL,2011-09-21 09:36:10.420,NULL,POSfasion,POSFASION,NULL,NULL,NULL,NULL,0,sdfa#ss.com,NULL,NULL,Maria,NULL,NULL,NULL,NULL,Aldelo for Restaurants demo download,T,NULL,becker4,BECKER4,L6UJ9A000004,NULL,NULL,NULL,this is another lead from the live site,NULL,NULL,NULL,NULL,NULL,SYST00000001,NULL,New,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,7778889999,QQ925684753 ,NULL,NULL,NULL,NULL
If there are characters after the 1 in the userField1 then try using a wildcard, as shown below.
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1%'
Also the statement you wrote
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 like '1'
Is essentially equivalent to
select * from [SalesLogix].[sysdba].[LEAD] where USERFIELD1 = '1'
You may want to read up on using the LIKE clause
http://msdn.microsoft.com/en-us/library/ms179859.aspx
Use
where USERFIELD1 = '1
'
You don't need LIKE as you aren't using any wild cards.
You're using LIKE wrong. Put a wildcard or a pattern in there or use an = operator.
Your data seems malformed.
Try a like with the value surrounded by a wildcard
ie USERFIELD1 like '1%'
Should sort out whatever problem you're having if it is related to the line break field