T-SQL: CASE statement in WHERE? - sql

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 + '%

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 use Case statement in SQL correctly

The below Select statement does NOT return a record for me:
Select 1:
SELECT
Case When rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join Bil_ManualPaymentDetails bmpd
on rcdr.PolicyNumber like '' + bmpd.PaymentReferenceNumber + '%' + ''
Output:
System =
The below Select statement DOES return a record for me:
Select 2:
SELECT
Case When rcdr.PolicyNumber like '1234567890%'
Then 'Adc' else 'Exceed' end as System
FROM RcDetailRecords rcdr
join BilManualPaymentDetails bmpd
on rcdr.PolicyNumber like '1234567890%'
Output:
System = Adc
Am I missing a tick '' mark somewhere in the first Select ?
BilManualPaymentDetails Table:
PaymentReferenceNumber
123456789020161013025120
RcDetailRecords Table:
PolicyNumber
1234567890
1234567890 is not like 123456789020161013025120%, but 123456789020161013025120 is like 1234567890%
So it appears you have the fields switched, try:
SELECT
CASE WHEN bmpd.PaymentReferenceNumber LIKE '' + rcdr.PolicyNumber + '%'
THEN 'Adc'
ELSE 'Exceed'
END as System
FROM RcDetailRecords rcdr
JOIN Bil_ManualPaymentDetails bmpd
ON bmpd.PaymentReferenceNumber like '' + rcdr.PolicyNumber + '%' + ''

sql order by best matched results from one specific where clause

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.

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 +' %')

Using LIKE within CASE in TSQL

I would like to select the records that contain the content of the #selctDescription parameter but only when #selctDescription is not empty.
I have the following, which does not work:
(t.[description] LIKE
(
CASE
WHEN #selctDescription = '' THEN t.[description]
ELSE ('%' #selctDescription '%')
END
)
)
Can anyone point me in the right direction?
SELECT *
FROM Table
WHERE
((#selctDescription IS NULL OR #selctDescription = '')
OR
(t.[description] LIKE '%' + #selctDescription +'%'))
#selctDescription = '' OR t.[description] LIKE '%' + #selctDescription + '%'
I think your code is equivalent to:
t.[description] LIKE '%' + #selctDescription + '%'
Your description on the other hand, suggests you want this:
#selctDescription <> ''
AND t.[description] LIKE '%' + #selctDescription + '%'
A couple of thoughts for you...
Where you have '%' #selctDescription '%', you just need + between the strings to concatenate them. Your code will then work as is.
'%' + #selctDescription + '%'
Also, it's useful to note that you don't even need the CASE statement, as when the parameter is blank, you get '%%', which will still match everything.
This is useful to know because at present you have table fields on both sides of the LIKE statement. This will really hurt the optimiser. If I were to use CASE I'd be more tempted to stick to...
t.[description] LIKE (CASE WHEN #selctDescription = '' THEN '%' ELSE ('%' + #selctDescription + '%') END)
This has the benefit that the result of the CASE statement can be performed once, as a scalar value, prior to execution of the query. As opposed to being recalculated ever row.
And that said, it then becomes functionally identical to...
t.[description] LIKE ('%' + #selctDescription + '%')
Based on your first line and comments, you need to make the following select:
Select * from table
where field <> ''
and field like '%' + #selctDescription + '%'
But you must put the correct table and field and #selctDesciption must be text (char, nchar, varchar, nvarchar, ...).