问题
I am implementing search operation (client side searching via stored procedure) i have a 'Nvarchar' type column 'ArticleHeader' and a 'DATE' column 'DateEffective' i am performing conditional based searching by passing @operator, for the fallowing snippet i am trying to search date and article header in Conjunction (AND) i am getting no results how ever if i run them individually they works out fine.
Find the snippet :
CREATE TABLE #TempItems
(
DateEffective DATE,
ArticleHeader NVARCHAR(50)
)
insert into #TempItems values ('2019-12-28','Nieuws')
insert into #TempItems values ('2020-02-12','Test')
insert into #TempItems values ('2020-01-10','zolo')
insert into #TempItems values ('2020-02-23','valued')
declare @DateEffective datetime,@operator nvarchar(10)
--set @DateEffective ='2020-01-10'
set @operator='neq'
declare @ArticleHeader nvarchar (50) ='Nieuws'
select * from #TempItems PA
Where 1=1
--------------------------------------------------------------------
And
(
(
(@operator='neq') And
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE convert(date,PA.DateEffective)
END !=
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE Convert(Date, @DateEffective)
End
)
OR
(
(@operator='eq') And
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE convert(date,PA.DateEffective)
END =
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE Convert(Date, @DateEffective)
End
)
)
----------------------------------------------------------------------
AND
(
(lower(@Operator) = 'eq' AND
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE PA.ArticleHeader
END =
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE CONVERT(nvarchar(1000), @ArticleHeader)
END
)
OR
(lower(@Operator) = 'startswith' AND
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE PA.ArticleHeader
END Like
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE CONVERT(nvarchar(1000), @ArticleHeader) + '%'
END
)
)
---------------------------------------------------------------------------
回答1:
Hi @Azhar can you please check this query it's working fine in both cases.
DROP TABLE IF EXISTS #TempItems
CREATE TABLE #TempItems
(
DateEffective DATE,
ArticleHeader NVARCHAR(50)
)
insert into #TempItems values ('2019-12-28','Nieuws')
insert into #TempItems values ('2020-02-12','Test')
insert into #TempItems values ('2020-01-10','zolo')
insert into #TempItems values ('2020-02-23','valued')
insert into #TempItems values ('2020-02-24','Nieuws')
declare @DateEffective datetime,@operator nvarchar(10)
set @DateEffective ='2020-01-10'
set @operator='startswith'
declare @ArticleHeader nvarchar (50) ='Nieuws'
select * from #TempItems PA
Where 1=1
--------------------------------------------------------------------
And
(
(
(@operator='neq') And
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE convert(date,PA.DateEffective)
END !=
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE Convert(Date, @DateEffective)
End
)
OR
(
(@operator='eq') And
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE convert(date,PA.DateEffective)
END =
CASE WHEN @DateEffective IS NULL
THEN '1900-1-1'
ELSE Convert(Date, @DateEffective)
End
)
OR
(
1=1
)
)
------------------------------------------------------------------------
AND
(
(lower(@Operator) = 'eq' AND
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE PA.ArticleHeader
END =
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE CONVERT(nvarchar(1000), @ArticleHeader)
END
)
OR
(lower(@Operator) = 'startswith' AND
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE PA.ArticleHeader
END Like
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE CONVERT(nvarchar(1000), @ArticleHeader) + '%'
END
)
OR
(lower(@Operator) = 'neq' AND
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE PA.ArticleHeader
END <>
CASE WHEN @ArticleHeader IS NULL
THEN '1'
ELSE CONVERT(nvarchar(1000), @ArticleHeader)
END
)
)
---------------------------------------------------------------------------
Output
Startwith
来源:https://stackoverflow.com/questions/60359272/sql-condition-based-searching-not-working-with-and-condition