February 8, 2018 at 9:59 am
Good Folks... My question is already in the code...
See the code ( the very last SQL statement )
Basically I am asking a question on how to use the LIKE operator with multiple values.
IF OBJECT_ID('tempdb..#tmpTbl') IS NOT NULL
DROP TABLE #tmpTbl;
IF OBJECT_ID('tempdb..#member') IS NOT NULL
DROP TABLE #member;
CREATE TABLE #member (
MemberId varchar(100), ID INT IDENTITY(1,1)
);
INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
UNION
Select '0020166055ABC'
UNION
Select '0001261781DEF'
INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
CREATE TABLE #tmpTbl (
BeneficiaryId varchar(100),
MatchingRowCountInMemberTbl int DEFAULT 0
);
INSERT INTO #tmpTbl( BeneficiaryId, MatchingRowCountInMemberTbl )
Select '0001261781', 2
UNION
Select '0020166055', 2
Select
M.*
FROM
#member M
INNER JOIN
#tmpTbl T ON ( M.MemberId = T.BeneficiaryId )
WHERE
T.MatchingRowCountInMemberTbl > 1
/*
I need the SQl statement modified so that it captures any MEMBERID in #member that will match the BeneficiaryID in #tmpTBL
So in other words, the SELECt stament should resturn the
'0020166055ABC','0001261781DEF'
*/
February 8, 2018 at 10:57 am
mw112009 - Thursday, February 8, 2018 9:59 AMGood Folks... My question is already in the code...
See the code ( the very last SQL statement )Basically I am asking a question on how to use the LIKE operator with multiple values.
IF OBJECT_ID('tempdb..#tmpTbl') IS NOT NULL
DROP TABLE #tmpTbl;IF OBJECT_ID('tempdb..#member') IS NOT NULL
DROP TABLE #member;CREATE TABLE #member (
MemberId varchar(100), ID INT IDENTITY(1,1)
);INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
UNION
Select '0020166055ABC'
UNION
Select '0001261781DEF'INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'CREATE TABLE #tmpTbl (
BeneficiaryId varchar(100),
MatchingRowCountInMemberTbl int DEFAULT 0
);INSERT INTO #tmpTbl( BeneficiaryId, MatchingRowCountInMemberTbl )
Select '0001261781', 2
UNION
Select '0020166055', 2Select
M.*
FROM
#member M
INNER JOIN
#tmpTbl T ON ( M.MemberId = T.BeneficiaryId )
WHERE
T.MatchingRowCountInMemberTbl > 1
/*
I need the SQl statement modified so that it captures any MEMBERID in #member that will match the BeneficiaryID in #tmpTBL
So in other words, the SELECt stament should resturn the
'0020166055ABC','0001261781DEF'
*/
select
m.*
from
#member as m
where
exists(select 1 from #tmpTbl as t where t.BeneficiaryId like m.MemberId + '%' and t.MatchingRowCountInMemberTbl > 1);
February 8, 2018 at 11:03 am
Lynn Pettis - Thursday, February 8, 2018 10:57 AMmw112009 - Thursday, February 8, 2018 9:59 AMGood Folks... My question is already in the code...
See the code ( the very last SQL statement )Basically I am asking a question on how to use the LIKE operator with multiple values.
IF OBJECT_ID('tempdb..#tmpTbl') IS NOT NULL
DROP TABLE #tmpTbl;IF OBJECT_ID('tempdb..#member') IS NOT NULL
DROP TABLE #member;CREATE TABLE #member (
MemberId varchar(100), ID INT IDENTITY(1,1)
);INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
UNION
Select '0020166055ABC'
UNION
Select '0001261781DEF'INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'CREATE TABLE #tmpTbl (
BeneficiaryId varchar(100),
MatchingRowCountInMemberTbl int DEFAULT 0
);INSERT INTO #tmpTbl( BeneficiaryId, MatchingRowCountInMemberTbl )
Select '0001261781', 2
UNION
Select '0020166055', 2Select
M.*
FROM
#member M
INNER JOIN
#tmpTbl T ON ( M.MemberId = T.BeneficiaryId )
WHERE
T.MatchingRowCountInMemberTbl > 1
/*
I need the SQl statement modified so that it captures any MEMBERID in #member that will match the BeneficiaryID in #tmpTBL
So in other words, the SELECt stament should resturn the
'0020166055ABC','0001261781DEF'
*/
select
m.*
from
#member as m
where
exists(select 1 from #tmpTbl as t where t.BeneficiaryId like m.MemberId + '%');
Lynn: I tried that .. No luck 🙁
February 8, 2018 at 11:06 am
mw112009 - Thursday, February 8, 2018 11:03 AMLynn Pettis - Thursday, February 8, 2018 10:57 AMmw112009 - Thursday, February 8, 2018 9:59 AMGood Folks... My question is already in the code...
See the code ( the very last SQL statement )Basically I am asking a question on how to use the LIKE operator with multiple values.
IF OBJECT_ID('tempdb..#tmpTbl') IS NOT NULL
DROP TABLE #tmpTbl;IF OBJECT_ID('tempdb..#member') IS NOT NULL
DROP TABLE #member;CREATE TABLE #member (
MemberId varchar(100), ID INT IDENTITY(1,1)
);INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
UNION
Select '0020166055ABC'
UNION
Select '0001261781DEF'INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'CREATE TABLE #tmpTbl (
BeneficiaryId varchar(100),
MatchingRowCountInMemberTbl int DEFAULT 0
);INSERT INTO #tmpTbl( BeneficiaryId, MatchingRowCountInMemberTbl )
Select '0001261781', 2
UNION
Select '0020166055', 2Select
M.*
FROM
#member M
INNER JOIN
#tmpTbl T ON ( M.MemberId = T.BeneficiaryId )
WHERE
T.MatchingRowCountInMemberTbl > 1
/*
I need the SQl statement modified so that it captures any MEMBERID in #member that will match the BeneficiaryID in #tmpTBL
So in other words, the SELECt stament should resturn the
'0020166055ABC','0001261781DEF'
*/
select
m.*
from
#member as m
where
exists(select 1 from #tmpTbl as t where t.BeneficiaryId like m.MemberId + '%');Lynn: I tried that .. No luck 🙁
Odd... but this worked ....
select
m.*
from
#member as m
where exists(select 1 from #TmpTbl as t where m.MemberId like t.BeneficiaryId + '%' );
February 8, 2018 at 11:12 am
mw112009 - Thursday, February 8, 2018 11:03 AMLynn Pettis - Thursday, February 8, 2018 10:57 AMmw112009 - Thursday, February 8, 2018 9:59 AMGood Folks... My question is already in the code...
See the code ( the very last SQL statement )Basically I am asking a question on how to use the LIKE operator with multiple values.
IF OBJECT_ID('tempdb..#tmpTbl') IS NOT NULL
DROP TABLE #tmpTbl;IF OBJECT_ID('tempdb..#member') IS NOT NULL
DROP TABLE #member;CREATE TABLE #member (
MemberId varchar(100), ID INT IDENTITY(1,1)
);INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'
UNION
Select '0020166055ABC'
UNION
Select '0001261781DEF'INSERT INTO #member ( MemberId )
Select '0001261781'
UNION
Select '0020166055'CREATE TABLE #tmpTbl (
BeneficiaryId varchar(100),
MatchingRowCountInMemberTbl int DEFAULT 0
);INSERT INTO #tmpTbl( BeneficiaryId, MatchingRowCountInMemberTbl )
Select '0001261781', 2
UNION
Select '0020166055', 2Select
M.*
FROM
#member M
INNER JOIN
#tmpTbl T ON ( M.MemberId = T.BeneficiaryId )
WHERE
T.MatchingRowCountInMemberTbl > 1
/*
I need the SQl statement modified so that it captures any MEMBERID in #member that will match the BeneficiaryID in #tmpTBL
So in other words, the SELECt stament should resturn the
'0020166055ABC','0001261781DEF'
*/
select
m.*
from
#member as m
where
exists(select 1 from #tmpTbl as t where t.BeneficiaryId like m.MemberId + '%');Lynn: I tried that .. No luck 🙁
You can try this but the issue with doing text matching like this is it'll break if you ever have over lap between member ids and whatever is in that beneficiary id after the member id part.
SELECT * FROM #tmpTbl
INNER JOIN #member ON CHARINDEX(#tmpTbl.BeneficiaryId, #member.MemberId) = 1 AND #tmpTbl.BeneficiaryId != #member.MemberId
February 8, 2018 at 1:29 pm
My fault, read things backwards when I was writing the code.
February 8, 2018 at 2:45 pm
Lynn Pettis - Thursday, February 8, 2018 1:29 PMMy fault, read things backwards when I was writing the code.
Thanks! Credit goes to you!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply