I have a textbox and 3 dropdown boxes with an item that could be selected in each of the dropdowns. There are two filtering scenarios I want to be able to achieve.
Getting a filtering result after typing a value in the textbox and select values in the next two textboxes leaving out the last one.
Getting a filtering after typing a value in the textbox and select values in the other three dropdown boxes to filter out the result. My code below can only produce each of the results one at a time If I change the "and" to "or" and the "or"s to "and"s. Can anyone help with modification or new query to be able to achieve the two scenarios?
CREATE PROC Spsearchproduct #searchWord1OnMasterPage NVARCHAR (50),
#searchWord2OnMasterPage NVARCHAR (50),
#searchWord3OnMasterPage NVARCHAR (50),
#searchWord4OnMasterPage NVARCHAR (50)
AS
BEGIN
SELECT product.NAME,
price,
seller,
productstreetno.strno,
productstreet.streetname
FROM product
INNER JOIN productstreetno
ON product.streetnoid = productstreetno.idstreetno
INNER JOIN productstreet
ON product.streetid = productstreet.idstreet
INNER JOIN productstate
ON stateid = productstate.idstate
INNER JOIN productcity
ON cityid = productcity.idcity
WHERE product.NAME LIKE '%' + #searchWord1OnMasterPage + '%'
AND productstate.statename LIKE '%' + #searchWord2OnMasterPage + '%'
AND ( ( productcity.cityname LIKE '%' + #searchWord3OnMasterPage + '%' )
OR ( productstreet.streetname LIKE '%' + #searchWord4OnMasterPage + '%' )
OR ( productstreet.streetname IS NULL ) )
AND ( ( productcity.cityname LIKE '%' + #searchWord3OnMasterPage + '%' )
OR ( productstreet.streetname LIKE '%' + #searchWord4OnMasterPage + '%' )
OR ( productstreet.streetname IS NULL ) )
END
Product.Name Like '%' + #searchWord1OnMasterPage + '%' and
ProductState.StateName Like '%' + #searchWord2OnMasterPage + '%' and
(ProductCity.CityName Like '%' + #searchWord3OnMasterPage + '%' )and
((ProductStreet.StreetName Like '%' + #searchWord4OnMasterPage + '%') or
(#searchWord4OnMasterPage = 'Select Street'))
Related
What I'm trying to do is I'm doing a quicksearch with multiple input so that if either any of the textbox is filled even when there are empty textbox, the data can still be retrieved.
The problem that I've faced is when I've tried to do the searching with date range the data failed to search when the date textbox of the date is empty.
Even when other textbox is not null.
I've also tried finding the date range using this method:
and (convert(varchar, DateIssue, 105) between #DateTo and #DateFrom )
next is query that I used for searching
SELECT * FROM [Table2]
where (ID like '%' + #ID + '%' or ID=#ID )
and(Pic_Mgr like '%' + #Picmgr + '%' or Pic_Mgr=#Picmgr)
and(DEPT like '%' + #dept + '%' or DEPT=#dept)
and ( DateIssue between #DateTo and #DateFrom )
next is the code behind that I used to get the parameter value:
cmd.Parameters.AddWithValue("#ID", txtid.Text)
cmd.Parameters.AddWithValue("#Dept", txtIssDept.Text)
cmd.Parameters.AddWithValue("#Picmgr", txtPICMgr.Text)
cmd.Parameters.AddWithValue("#DateTo", txtdateto.Text)
cmd.Parameters.AddWithValue("#DateFrom", txtdatefrm.Text)
I've tried to do the searching without the date range and it work just fine.So I assume there might be problem with my query regarding handling the datetime data .
Is there any suggestion on how to fix this ??
SELECT * FROM [Table2]
where (ID like '%' + #ID + '%' or ID=#ID )
and(Pic_Mgr like '%' + #Picmgr + '%' or Pic_Mgr=#Picmgr)
and(DEPT like '%' + #dept + '%' or DEPT=#dept)
and (DateIssue between
(case when isnull(#DateTo,'') <>'' then #DateTo else DateIssue end) and
(case when isnull(#DateFrom,'') <>'' then #DateFrom else DateIssue end))
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'm writing a stored procedure about searching the database based on aspx page textbox input text.
This stored procedure works well, but I have a problem.
Sometimes #DeptName and #DutyName parameters are empty or null.
So I want to skip or ignore where clauses when that parameter values are empty. But I am confused as to how to handle this situation.
Please give me your advice.
My stored procedure code:
DECLARE
#CompanyCode varchar(20)
, #DeptName nvarchar(20)
, #DutyName nvarchar(20)
SELECT
#CompanyCode = 'h101'
, #DeptName = 'IT'
, #DutyName = 'Manager'
SELECT
U.ADDisplayName AS UserName
, LOWER(U.UserID) AS EmpID
, ISNULL(CPN.CompanyName, '') AS CompanyCode
, ISNULL(U.DisplayName_Eng, '') AS DisplayName_Eng
, ISNULL(DT.DisplayName, '') AS DeptName
, ISNULL(DTY.DutyName, '') AS DutyName
, ISNULL(U.CellPhone, '') AS CellPhone
, ISNULL(U.ExtensionNumber, '') AS ExtensionNumber
, ISNULL(U.FaxNumber, '') AS FaxNumber
, ISNULL(U.ChargeJob, '') AS ChargeJob
, ISNULL(LOC.LocationName, '') AS LocationName
, ISNULL(U.Workplace, '') AS Workplace
, ISNULL(U.EMail, '') AS EMail
FROM dbo.tb_User U
INNER JOIN dbo.tb_Dept DT
ON U.MainDeptCode = DT.DeptCode
INNER JOIN dbo.tb_Company CPN
ON U.CompanyCode = CPN.CompanyCode
INNER JOIN dbo.tb_Location LOC
ON U.LocationCode = LOC.LocationCode
AND U.CompanyCode = LOC.CompanyCode
AND U.GroupCode = LOC.GroupCode
AND U.DetailCode = loc.DetailCode
INNER JOIN dbo.tb_Duty DTY
ON U.DutyCode = DTY.DutyCode
AND U.CompanyCode = DTY.CompanyCode
AND U.GroupCode = DTY.GroupCode
AND U.DetailCode = DTY.DetailCode
WHERE U.CompanyCode = #companyCode
AND DT.DisplayName like '%' + #DeptName + '%'
AND DTY.DutyName like '%' + #DutyName + '%'
Order by DeptName desc
Thank you.
A very common construction is :
WHERE U.CompanyCode = #companyCode
AND (#DeptName is null or DT.DisplayName like '%' + #DeptName + '%')
AND (#DutyName is null or DTY.DutyName like '%' + #DutyName + '%')
...
...
with (recompile)
The with (recompile) hint at the end of your statement is telling SQL Server to check the optimization plan "after" replacing the variables by their values. Doing so it allows to completely ignore a condition when its parameter is null, resulting normally in an optimal execution (only on very complex statements you will need to do more in order to help the SQL engine to find the better execution plan).
It's also worth noting that using the with (recompile) clause forces to check for a new plan at every execution (instead of reusing an existing one), so if your sentence is going to be executed several times for second, then you will be better suited using an alternative, like parameterized dynamic SQL. Although this is not the most usual situation.
PS: If your parameter can also be an empty string, then use isnull to check both options. You still need to add the recompilation hint to execute it optimally.
WHERE U.CompanyCode = #companyCode
AND (isnull(#DeptName, '') = '' or DT.DisplayName like '%' + #DeptName + '%')
AND (isnull(#DutyName, '') = '' or DTY.DutyName like '%' + #DutyName + '%')
...
...
with (recompile)
Add an extra #DeptName is null and #DeptName ="" to each clause:
WHERE U.CompanyCode = #companyCode
AND ((DT.DisplayName like '%' + #DeptName + '%') or (#DeptName is null) or (#DeptName =''))
AND ((DTY.DutyName like '%' + #DutyName + '%') or (#DeptName is null) or (#DeptName = ''))
Order by DeptName desc
You can try this.
Using ISNULL you can replace NULL with a specified replacement value.
In this case, IF #DeptName is null then replace it with value of DT.DisplayName. same applies to #DutyName.
The Logical Function IIF is used to check empty value but this applies starting SQL2012 version, otherwise CASE expression for SQL2008 or later versions.
WHERE U.CompanyCode = #companyCode
AND DT.DisplayName like '%' + ISNULL(IIF(#DeptName = '',NULL,#DeptName),DT.DisplayName) + '%'
AND DTY.DutyName like '%' + ISNULL(IIF(#DutyName = '',NULL,#DutyName),DTY.DutyName) + '%'
Order by DeptName desc
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 got the MS SQL stored procedure, which is designed to return the results in XML showing all the applicants who are 'sales'.
There are filters on the page to drill down the results one being a search (sString).
The desired behavior is:
If sString is null, it should return all the results;
If sString is not null, it should make sString to the show the found fields.
The SP does not work as expected: it always returns all the results regardless of sString value.
MS SQL Code of the SP:
WITH OrderedMatches AS
(
SELECT ROW_NUMBER() OVER (ORDER BY MB.Member_Registered DESC) AS RowNumber,
MB.Member_ID,
MB.Member_Name, MB.Member_Mobile, MB.Member_Propertytosell, MB.Member_Propertytosell_Details, MB.Member_ExistingBuytoLet, MB.Member_TalkingPoints,
MB.Member_Registered, MB.Member_Funding, MB.Member_Active, MB.Member_Last_Contacted, MB.Member_Situation
FROM dbo.Member_Base MB INNER JOIN dbo.Member_Criteria MC ON MC.Member_ID = MB.Member_ID
WHERE MB.Member_Active = 1 AND MC.Criteria_Section = 'sales'
AND (
#sType = 'a'
OR (
#sType = 'b' AND MB.Member_Propertytosell = 1
)
OR (
#sType = 'c' AND MB.Member_ExistingBuytoLet = 1
)
)
OR (
MB.Member_Name LIKE '%' + #sString + '%' OR MB.Member_Mobile LIKE '%' + #sString + '%' OR MB.Member_Propertytosell_Details LIKE '%' + #sString + '%'
)
)
SELECT
(
SELECT
OM.Member_ID as "#id",
OM.Member_Name as "#appname",
OM.Member_Mobile as "#contact",
OM.Member_Propertytosell as "#propts",
OM.Member_Propertytosell_Details as "#proptsdetails",
OM.Member_ExistingBuytoLet as "#existingBTL",
OM.Member_TalkingPoints as "#talkingpoints",
OM.Member_Registered as "#registered",
OM.Member_Funding as "#funding",
OM.Member_Active as "#active",
OM.Member_Last_Contacted as "#lastcontact",
OM.Member_Situation as "#situation"
FROM OrderedMatches OM
WHERE OM.RowNumber Between #nstart AND #nend
FOR XML path ('applicant'), TYPE
),
(
SELECT COUNT(*) as "#titems"
FROM OrderedMatches
FOR XML path ('meta')
)
FOR XML PATH ('')
END
I suppose the SP is wrong but cant see at which part exactly.
Does anyone have any suggestions?
It's returning everything because you have OR between your sString condition and the rest of conditions in WHERE. Change your WHERE clause to:
WHERE MB.Member_Active = 1 AND MC.Criteria_Section = 'sales'
AND (
#sType = 'a'
OR (
#sType = 'b' AND MB.Member_Propertytosell = 1
)
OR (
#sType = 'c' AND MB.Member_ExistingBuytoLet = 1
)
)
AND (#sString IS NULL
OR
(
MB.Member_Name LIKE '%' + #sString + '%' OR MB.Member_Mobile LIKE '%' + #sString + '%' OR MB.Member_Propertytosell_Details LIKE '%' + #sString + '%'
)
)