I'm writing an sql server query:
select * from vwApplicationList
where status='Unassigned' AND
BBL like '%' + #BBL + '%' AND
UnitOrAppartmentNumber like '%'+#Appartment+'%' AND
ResidenceTypeDescription like '%'+#ResidenceTypeDescription+'%' AND
SN1 like '%'+#SN1+'%' AND
SN2 like '%'+#SN2+'%'
The problem is that the field "SN2" is null in several records. So how can I compare them?
you can do
COALESCE(SN2 ,'') like '%'+#SN2+'%'
COALESCE
If you want a positive result on the comparison:
... AND COALESCE(SN2, #SN2) LIKE '%' + #SN2 + '%'
If you want a negative result (assuming #SN2 is not also an empty string):
... AND COALESCE(SN2, '') LIKE '%' + #SN2 + '%'
NULL and only be compared with IS NOT and IS NOT NULL.
If you want to always include NULL you could try
(SN2 like '%'+#SN2+'%' OR SN2 IS NULL)
If you only want to include NULL when #SN2 is empty, try something like
(SN2 like '%'+#SN2+'%' OR (#SN2 = '' AND SN2 IS NULL))
Related
I'm looking to perform a combined search based on fixed, known references (Int) and also wildcard's (varchar) on different columns.
My integer search works fine, but by varchar (LIKE) search fails with a varchar to int conversion failure.
WHERE
MyID = 1 AND MyRef = #Search OR
MyID = 1 AND MyName LIKE N'%' + #Search + N'%'
I'm assuming that the failure is when SQL is searching for a varchar against the int (MyRef)
Where have I gone wrong ?
thanks in advance
A simple method is to use concat():
WHERE MyID = 1 AND
( MyRef = #Search OR
MyName LIKE CONCAT('%', #Search , '%')
)
However, something doesn't seem comfortable about this. Do you really want the same search term for both fields?
Does this return the expected result?
where MyId = 1 and
case isnumeric(#search) when 1 then
case when myRef = #search then 1 else 0 end
else
case when MyName like N'%' + #search + N'%' then 1 else 0 end
end = 1
or
where MyId = 1 and
case try_cast(#search as int) when is not null then
case when myRef = #search then 1 else 0 end
else
case when MyName like N'%' + #search + N'%' then 1 else 0 end
end = 1
SQL Server will try to implicitly convert strings to numbers whenever you compare, move or concatenate a number to a string.
To avoid it, you must explicitly convert the number to a string:
WHERE MyId = 1
AND
(
MyRef = #Search
OR MyName LIKE N'%' + CAST(#Search as varchar(11)) + N'%'
)
I'm using varchar(11) because it's just big enough to hold the minimum value of int, including the minus sign. If the number is of a different type, you might want to choose a different size.
I've also rearranged your conditions a little bit - there's no point of having the same condition multiple times, and also, it's always good to use parenthesis when combining and and or in the same where clause.
Thanks to all.
I decided against combining all search parameters into one, instead i've split out the reference search and the text search.
I have a stored procedure which is called from a program. This stored procedure is passed 2 names and needs to find similar names in the database. However when I try to compare - 'AFC Bournemouth' with the name in the db which is 'Bournemouth'.
bet.searchFixtureForResult
#homeTeam NVARCHAR(MAX),
#awayTeam NVARCHAR(MAX),
#date DATE
AS
SELECT fixtureId
FROM bet.fixture
WHERE homeTeam IN (SELECT teamId
FROM bet.team
WHERE team LIKE '%' + #homeTeam + '%')
AND awayTeam IN (SELECT teamId
FROM bet.team
WHERE team LIKE '%' + #awayTeam + '%')
AND fixtureDate = #date
This supposedly returns the Id of the fixture that falls within the parameters given. How can compare similar strings such that I compare in the same way you compare with .Contains in C#?
I don't think you need bet.team there at all, your query will be like
SELECT fixtureId
FROM bet.fixture
WHERE (HomeTeam LIKE '%' + #HomeTeam + '%' OR #HomeTeam LIKE '%' + HomeTeam + '%')
AND
(AwayTeam LIKE '%' + #AwayTeam + '%' OR #AwayTeam LIKE '%' + AwayTeam + '%')
AND
fixtureDate = #date;
Here is a little Demo
I've got a sql statement in a SQL Server stored procedure using a where clause like this:
Select (fields)
From Table
Where Testname like #SearchLetter + '%'
I need to do check the parameter #SearchLetter and if it's "#", I need to make the parameter choose records that the field Testname starts with a numeric field (0-9)
I've tried several things (ifs/selects, etc.) but I can't seem to get what I need.
anyone know how I can do this?
Try
where Testname like case #SearchLetter
when '#' then '[0-9]' + '%'
else #Searchletter + '%' end
You can use
Where Testname like REPLACE(#SearchLetter, '#', '[0-9]') + '%'
Assuming that the string is always one character (or that you always want this substitution to occur wherever the hash falls) Otherwise use a CASE statement to check for exact equality.
Where Testname like
CASE WHEN #SearchLetter = '#'
THEN '[0-9]'
ELSE #SearchLetter
END + '%'
The simplest way is
where left(#SearchLetter, 1) between '0' and '9
How can a SSRS wildcard search be stopped from returning all values when left blank? Currently, if I enter a value for SKU (generated by the script below) and don't enter at least one value for SKU_Description the report returns all records rather than just the SKU record.
I have parameters set to allow blanks and multiple values because I need the ability to enter either an SKU or an SKU_Description or both, depending on what is know about each product.
WHERE A.sku in (#SKU)
OR B.sku_desc like '%' + #SKU_Description + '%'
I thought something like this might work, but it doesn't:
IIf(Parameters!SKU_Description.Value="", "WHERE A.sku in (#SKU)"
, "WHERE A.sku in (#SKU) or B.sku_desc like '%' + #SKU_Description + '%'")
Every thread I can find is about returning all values when left blank, which is the opposite of what I need to do.
your query might need to look like this instead.
WHERE (#SKU = '' OR A.sku in (#SKU))
AND (#SKU_Description = '' OR B.sku_desc like '%' + #SKU_Description + '%')
if #SKU or #SKU_Description can be NULL then you'll probably want
WHERE (ISNULL(#SKU,'') = '' OR A.sku in (#SKU))
AND (ISNULL(#SKU_Description,'') = '' OR B.sku_desc like '%' + #SKU_Description + '%')
Edit:
you can use this if the Sku and SKU_Description dont have to match for same record.
WHERE A.sku in (#SKU)
OR (#SKU_Description <> '' AND B.sku_desc like '%' + #SKU_Description + '%')
I have a search functionality in my application which searches a string value of a particular column. The search text is passed as a parameter to the procedure. Now I have to search in another column which is an integer from another table. The input param is one but now I have to search in both the columns and return the result according to that.
//Query:
#SearchText VARCHAR(8000) = ''
SELECT DISTINCT Id, TransactionId, wfdFieldValue
where (wfdFieldValue LIKE '%' + #SearchText + '%' OR #SearchText = '')
In the above query I have to include the TransactionId in the where condition. So that if the user searched using the TransactionId or the Fieldvalue the query will return the result.
Note: The TransactionId is an Integer DataType.
If I understand you correctly, you'll want a hit in either column to make the row show up in search results. In that case, you'll need a simple OR;
SELECT DISTINCT Id, TransactionId, wfdFieldValue
FROM bop
WHERE #SearchText='' OR
wfdFieldValue LIKE '%' + #SearchText + '%' OR
TransactionId LIKE '%' + #SearchText + '%'
(You can just use LIKE on the integer column right away without manually converting.)
Of note though, this kind of wildcard search with a leading % does not use indexes in an optimal way, performance of this query will most likely not be great as the table grows.
SELECT DISTINCT Id, TransactionId, wfdFieldValue
where wfdFieldValue LIKE '%' + #SearchText + '%'
OR #SearchText = ''
OR CAST(TransactionId AS VARCHAR(50)) LIKE '%' + #SearchText + '%'
A cast should solve it, but I would first think about possible design improvements.