sql order by best matched results from one specific where clause - sql

Hi I'm using sql server 2008 and have a big stored procedure which has multiple where clauses. Within one of those clause I have the following:
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
I'm looking to show results in an order, whereby I get the results where BOTH matched in clause 7 first, followed by if one matched. Is this possible? I've tried a few things but nothing has worked so far. E.g:
SELECT
BLAH BLAH
FROM MULTIPLE TABLE JOINS
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
ORDER BY
(CASE WHEN COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%' THEN 1 ELSE 0 END) DESC
and
SELECT BLAH BLAH, (case when COL1 LIKE '%'+ #VARIABLE + '%' then 1 else 0 end) +
(case when COL2 LIKE '%'+ #VARIABLE + '%' then 1 else 0 end) as [priority]
FROM MULTIPLE TABLE JOINS
WHERE clause1
AND (clause2)
AND (clause3)
AND (clause4)
AND (clause5)
AND (clause6)
AND ( COL1 LIKE '%' + #VARIABLE + '%' OR COL2 LIKE '%' + #VARIABLE + '%')
ORDER BY [Priority]
And a few more but nothing has worked...

Your first query is ordering based on your condition:
CASE WHEN COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%' THEN 1 ELSE 0 END
But it's redundant with your WHERE clause:
COL1 LIKE '%'+ #VARIABLE + '%' AND COL2 LIKE '%' + #VARIABLE + '%'
Add the CASE statement from your ORDER BY to your SELECT and you'll see it's 1 for every row, so the ORDER BY is working as it should, it just has no impact since all records are being ordered by 1, you need to alter the ORDER BY criteria, if you give additional detail about how you want it ordered someone can surely help.

Related

SQL ignore condition clause if the value is null or empty

I have the following query
SELECT id, namn, postA, postB postN FROM k.dbo.PF WHERE
namn LIKE #name + '%' AND
postA LIKE #address + '%' AND
postB LIKE #coAddress + '%' AND
postN LIKE #zip + '%' AND
k.dbo.status = 0
This query works as long as I have the correct values in all the fields. But in this case, the care of address (postB column) can sometimes be null in the database. But when I provide the parameter #coAddress with a null value, the query doesn't return anything. How can I rewrite this query so that it will skip the AND clause completely if the coAddress parameter is null?
Use OR:
SELECT id, namn, postA, postB postN
FROM k.dbo.PF
WHERE (#name is null OR namn LIKE #name + '%') AND
(#address is null OR postA LIKE #address + '%') AND
(#coAddress is null OR postB LIKE #coAddress + '%') AND
(#zip is null OR postN LIKE #zip + '%') AND
k.dbo.status = 0;
Try:-
SELECT id, namn, postA, postB postN FROM k.dbo.PF WHERE
namn LIKE #name + '%' AND
postA LIKE #address + '%' AND
(#coAddress is NULL or postB LIKE #coAddress + '%') AND
postN LIKE #zip + '%' AND
k.dbo.status = 0
Any expression involving a NULL value will always return false except for "IS NULL".

How to create Dynamic SQL based on two tables

I'm trying to construct a dynamic SQL based on the Cartesian product of two tables
Table1 Colunm1 Dev Test
table2 Column2 ProductNumber ProductDesc
here the result that I want:
(ProductNumber LIKE '%dev%' OR ProductDesc LIKE '%dev%' )
AND (ProductNumber LIKE '%Test%' OR ProductDesc LIKE '%Test%')
I tried to do some simple query like below but I cannot manage to add a AND instaed a OR between each column1 value
SELECT #sql = COALESCE(#sql + Colunm2 + ' LIKE ''%' + Colunm1 + '%'' OR ','')
from Table1, Table2
that give:
ProductNumber LIKE '%dev%' OR
ProductDesc LIKE '%dev%' OR
ProductNumber LIKE '%Test%' OR
ProductDesc LIKE '%Test%'
I can do it with a while but if you have a better solution I can use it
I note that you want your ORs grouped by table1.column1, so the below should work:
SELECT CASE WHEN row_num = 1 THEN ') AND (' ELSE '' END || code as code
FROM (
SELECT Column1, ROW_NUMBER() OVER (GROUP BY Column 1) as row_num, COALESCE( Column2 + ' LIKE ''%' + Column1 + '%'' OR ','') as code
FROM Table1, Table2
) gen_code
ORDER BY Column1, row_num
(I haven't tested it, but I have written lots of such code before)
It adds an additional ') AND (' at the beginning, but you can get it away if you use another ROW_NUMBER over the whole thing. It also lacks a closing ')', but you get the idea. Other than that, use your current approach with a variant of above code. Note that I assumed you have no string aggregation function available.

Adding a WHERE statement to an SQL query based on an IF statement

Issue: I only want to filter the PropertySearch value if there is one
I want to be able to have a dynamic SQL statement based on this.
I have added If #PropertySearch which filters from a textbox on the webform.
The search works up to -- If #PropertySearch <> '' -- and will work if I comment the code
--
If #PropertySearch <> ''
BEGIN
TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %'
END
--
I want to only filter the PropertyID/PropertySearch when there is a #PropertySearch.
I have looked at having 'AND' after 'BEGIN' as well as Nested tables but am struggling
If #RegionID = 1 --then -- Head office users
BEGIN
SELECT TblA.PropertyID as PId, TblA.Propertyname as PNa, TblB.FireSafetyDisplay as FireSafety1, TblB.SlipsandTripsDisplay as SaT
FROM TbPropertyDetails as TblA inner join TbPropertyDetailsSafeguarding as TblB on TblA.PropertyID = TblB.PropertyID
WHERE TblA.RegionID > 0
If #PropertySearch <> ''
BEGIN
TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %'
END
END
WHERE TblA.RegionID > 0 AND (#PropertySearch = ''
OR TblA.PropertyID LIKE '%' + #PropertySearch + '%' OR TblA.Propertyname LIKE '%' + #PropertySearch +' %')

SQL Using Case in Where clause for null values

I have a SQL query that I am trying to incorporate the possibility of null responses in my selections.
Ultimately, this will end up in a SSRS report.
This query works fine, but any null values in p.ReferralReason will always be returned. I would like the nulls to not be returned if the value of #Reason is anything but '%':
DECLARE #Reason varchar(100)
SET #Reason = 'Lost To Care'
SELECT p.Person_ID, P.Person_Name, p.ReferralReason
FROM VIEW_Patient p
WHERE
p.ReferralReason like '%' + #Reason + '%'
I would like to incorporate all reasons with the #Reason = '%'
If #Reason is set to '%', I would like to include the null values, but I do not want to include the null values if #Reason is set to anything else.
This is what I have tried, but it does not work:
DECLARE #Reason varchar(100)
SET #Reason = '%'
SELECT p.Person_ID, P.Person_Name, p.ReferralReason
FROM VIEW_Patient p
WHERE
case
when #Reason = '%' then (p.ReferralReason like '%' + #Reason + '%' or p.ReferralReason is null)
else p.ReferralReason like '%' + #Reason + '%'
end
MS SQL Server 2008 R2.
If the NULL values are replaced with empty strings they will match patterns consisting entirely of % but not other strings surrounded with %
SELECT p.Person_ID,
P.Person_Name,
p.ReferralReason
FROM VIEW_Patient p
WHERE ISNULL(p.ReferralReason, '') LIKE '%' + #Reason + '%'

T-SQL: CASE statement in WHERE?

I have the following WHERE clause in my SQL query currently:
WHERE c.Town LIKE '%' + #Town + '%'
What I want do is make the first '%' optional - so if the user has selected to just filter by records that just start with the given text, the WHERE would be
WHERE c.Town LIKE #Town + '%'
I assumed the simplest way to do this would be CASE statements, but I'm having no joy as it seems the syntax is incorrect. Can I get any pointers? Here's the CASE statement I was trying:
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '') + #Town + '%'
You missed the END:
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%'
A CASE must end with an END. Try
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%'
Case should have end.
I think the below statement can help you.
WHERE c.Town LIKE (CASE WHEN #FilterOptions = 1 THEN '%' ELSE '' END) + #Town + '%