SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Help me with the SELECT statement please ?


Help me with the SELECT statement please ?

Author
Message
mw112009
mw112009
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10402 Visits: 1182
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'
*/


Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)

Group: General Forum Members
Points: 239390 Visits: 40662
mw112009 - Thursday, February 8, 2018 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'
*/




select
m.*
from
#member as m
where
exists(select 1 from #tmpTbl as t where t.BeneficiaryId like m.MemberId + '%' and t.MatchingRowCountInMemberTbl > 1);



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
mw112009
mw112009
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10402 Visits: 1182
Lynn Pettis - Thursday, February 8, 2018 10:57 AM
mw112009 - Thursday, February 8, 2018 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'
*/




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 Sad

mw112009
mw112009
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10402 Visits: 1182
mw112009 - Thursday, February 8, 2018 11:03 AM
Lynn Pettis - Thursday, February 8, 2018 10:57 AM
mw112009 - Thursday, February 8, 2018 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'
*/




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 Sad

Odd... but this worked ....

select
m.*
from
#member as m
where exists(select 1 from #TmpTbl as t where m.MemberId like t.BeneficiaryId + '%' );

ZZartin
ZZartin
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13973 Visits: 14588
mw112009 - Thursday, February 8, 2018 11:03 AM
Lynn Pettis - Thursday, February 8, 2018 10:57 AM
mw112009 - Thursday, February 8, 2018 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'
*/




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 Sad

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

Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)SSC Guru (239K reputation)

Group: General Forum Members
Points: 239390 Visits: 40662
My fault, read things backwards when I was writing the code.

Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
mw112009
mw112009
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10402 Visits: 1182
Lynn Pettis - Thursday, February 8, 2018 1:29 PM
My fault, read things backwards when I was writing the code.

Thanks! Credit goes to you!

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search