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, ...).
Related
I have a query where I want to use something like IF conditional into WHERE clause so I do something like:
...
AND (#City = '%'
OR [a].[City] LIKE(#City))
So I read this like : IF #City = '%' just continue , if not execute OR clause OR [a].[City] LIKE(#City)
But when I run code results don't return values as I want. What am I doing wrong? am I reading this wrong?
You should be able to simply do:
WHERE . . . AND
[a].[City] LIKE #City
% is the wildcard for LIKE, so CITY LIKE '%' returns all non-NULL values of CITY. This seems to be the intention of using '%' for selecting all cities.
try like below:
...
AND (#City = '%'
OR [a].[City] LIKE('%'+ #City + '%'))
try this on your where clause:
WHERE #City = '%' OR [a].[City] LIKE '%' + #City + '%'
I needs to create a query to the database that will filter the results like this:
[ProjectNumber] LIKE
CASE WHEN IsNull(#ProjectId,'') = '' THEN
'%'
ELSE
#ProjectId + '%'
END
AND
[CountryCode] LIKE
CASE WHEN IsNull(#Country,'') = '' THEN
'%'
ELSE
#Country + '%'
END
When both variables have value everything works, but if #Country is null then this query returns all results for which in the CountryCode column there is a value, and I need to return all (even if the field is Null). Anyone know how to write this query in case of null variable that returned all the fields (fields with value and fields with null)?
Instead of using CASE ... WHEN ... and ISNULL function, you can do this:
...
(#ProjectId IS NULL OR [ProjectNumber] LIKE #ProjectId + '%')
AND
(#Country IS NULL OR [CountryCode] LIKE #Country + '%')
...
You need to simply use
ProjectNumber LIKE IsNull(#ProjectId,'') + '%' AND
CountryCode LIKE IsNull(#Country,'') + '%'
You don't need to use the case statement. Try the below :
ISNULL([ProjectNumber],'') LIKE IsNull(#ProjectId,'')+'%'
AND
ISNULL([CountryCode],'') LIKE ISNULL(#Country,'') + '%'
because both your case statements will give the same effect above.
I have the below code to match on similar characters where possible and it only brings through results from subquery A. Please can someone assist? Thanks
select
*
from
(
Select 'Test' T
)a
left join
(
Select 'Test1' T
)b
on
'%' + a.t + '%'
like
'%' + b.t + '%'
The like pattern only goes on the right side of the operator. I think you intend:
on (a.t like '%' + b.t + '%') or
(b.t like '%' + a.t + '%')
I was trying to find line breaks and carriage returns in a column in a SQL table and I am not sure about the syntax.
I tried:
SELECT foo FROM test
WHERE foo LIKE CHAR(10)
I didn't get any results even though I know that the table should return results. What am I doing wrong?
SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
Edit: to find all various types of line endings you should probably just check both:
SELECT foo FROM test WHERE foo LIKE '%' + CHAR(10) + '%'
OR foo LIKE '%' + CHAR(13) + '%'
SELECT foo FROM Table WHERE foo LIKE '%' + CHAR(13)+CHAR(10) + '%'
Try
SELECT foo FROM test WHERE foo LIKE '%'+ CHAR(10)+'%'
Try
SELECT foo FROM test WHERE foo LIKE CONCAT('%', CHAR(10), '%')
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 + '%