如何在没有动态 sql (SQL Server 2012) 的情况下使用 patindex 过滤记录
How to filter records using patindex without dynamic sql (SQL Server 2012)
如何在 sql 服务器中使用 patindex 过滤记录(没有动态 sql)
例如:
我有像
这样的记录
ID Name LastName Indicator
1 xxx Lxxx NULL
2 yyy Lyyy Yes
3 zzz Lzzz No
if @ind is null, i want to display all 3 records, if @ind ='Yes' then 2nd record should filter
if @ind = 'No' then 3rd record only should filter.
please suggest some good solution and it should not affect performance.
sample:
declare @ind varchar(10) = 'Yes' (can be null, yes or No)
select * from
(select 1 ID, 'xxx' Name,'Lxxx' LastName, NULL Indicator union all
select 2 ID, 'yyy' Name,'Lyyy' LastName, 'Yes' Indicator union all
select 3 ID, 'zzz' Name,'Lzzz' LastName, 'No' Indicator) x
where patindex('%' + isnull(@ind,'%') + '%', COALESCE(@ind,'%',Indicator)) > 0
您可以在 Where
子句中使用 Or
和 And
运算符来完成此操作。试试这个。
DECLARE @ind VARCHAR(10) = 'no' --(can be null, yes or No)
SELECT *
FROM (SELECT 1 ID,'xxx' Name,'Lxxx' LastName,NULL Indicator
UNION ALL
SELECT 2 ID,'yyy' Name,'Lyyy' LastName,'Yes' Indicator
UNION ALL
SELECT 3 ID,'zzz' Name,'Lzzz' LastName,'No' Indicator) x
WHERE ( @ind = 'Yes'
AND name = 'yyy' )
OR ( @ind = 'no'
AND name = 'zzz' )
OR ( @ind IS NULL )
如果你想使用 Patindex
使用这个。
DECLARE @ind VARCHAR(10) = 'yes' --(can be null, yes or No)
SELECT *
FROM (SELECT 1 ID,'xxx' Name,'Lxxx' LastName,NULL Indicator
UNION ALL
SELECT 2 ID,'yyy' Name,'Lyyy' LastName,'Yes' Indicator
UNION ALL
SELECT 3 ID,'zzz' Name,'Lzzz' LastName,'No' Indicator) x
WHERE ( patindex('Yes',@ind )>0
AND name = 'yyy' )
OR ( patindex('no',@ind )>0
AND name = 'zzz' )
OR ( @ind IS NULL )
我不明白为什么你必须使用 patindex,但如果你不这样做,这应该可以解决问题
declare @ind varchar(10) = 'Yes' (can be null, yes or No)
select * from
(select 1 ID, 'xxx' Name,'Lxxx' LastName, NULL Indicator union all
select 2 ID, 'yyy' Name,'Lyyy' LastName, 'Yes' Indicator union all
select 3 ID, 'zzz' Name,'Lzzz' LastName, 'No' Indicator) x
where (@ind is null or indicator = @ind)
如何在 sql 服务器中使用 patindex 过滤记录(没有动态 sql)
例如: 我有像
这样的记录ID Name LastName Indicator
1 xxx Lxxx NULL
2 yyy Lyyy Yes
3 zzz Lzzz No
if @ind is null, i want to display all 3 records, if @ind ='Yes' then 2nd record should filter
if @ind = 'No' then 3rd record only should filter.
please suggest some good solution and it should not affect performance.
sample:
declare @ind varchar(10) = 'Yes' (can be null, yes or No)
select * from
(select 1 ID, 'xxx' Name,'Lxxx' LastName, NULL Indicator union all
select 2 ID, 'yyy' Name,'Lyyy' LastName, 'Yes' Indicator union all
select 3 ID, 'zzz' Name,'Lzzz' LastName, 'No' Indicator) x
where patindex('%' + isnull(@ind,'%') + '%', COALESCE(@ind,'%',Indicator)) > 0
您可以在 Where
子句中使用 Or
和 And
运算符来完成此操作。试试这个。
DECLARE @ind VARCHAR(10) = 'no' --(can be null, yes or No)
SELECT *
FROM (SELECT 1 ID,'xxx' Name,'Lxxx' LastName,NULL Indicator
UNION ALL
SELECT 2 ID,'yyy' Name,'Lyyy' LastName,'Yes' Indicator
UNION ALL
SELECT 3 ID,'zzz' Name,'Lzzz' LastName,'No' Indicator) x
WHERE ( @ind = 'Yes'
AND name = 'yyy' )
OR ( @ind = 'no'
AND name = 'zzz' )
OR ( @ind IS NULL )
如果你想使用 Patindex
使用这个。
DECLARE @ind VARCHAR(10) = 'yes' --(can be null, yes or No)
SELECT *
FROM (SELECT 1 ID,'xxx' Name,'Lxxx' LastName,NULL Indicator
UNION ALL
SELECT 2 ID,'yyy' Name,'Lyyy' LastName,'Yes' Indicator
UNION ALL
SELECT 3 ID,'zzz' Name,'Lzzz' LastName,'No' Indicator) x
WHERE ( patindex('Yes',@ind )>0
AND name = 'yyy' )
OR ( patindex('no',@ind )>0
AND name = 'zzz' )
OR ( @ind IS NULL )
我不明白为什么你必须使用 patindex,但如果你不这样做,这应该可以解决问题
declare @ind varchar(10) = 'Yes' (can be null, yes or No)
select * from
(select 1 ID, 'xxx' Name,'Lxxx' LastName, NULL Indicator union all
select 2 ID, 'yyy' Name,'Lyyy' LastName, 'Yes' Indicator union all
select 3 ID, 'zzz' Name,'Lzzz' LastName, 'No' Indicator) x
where (@ind is null or indicator = @ind)