Hi
I would like to perform a procedure
which takes a string of numbers and searches for strings in a table which include all the
given numbers in any particular order. problem is i can only return strings
which include 'any' of the letters in the search string and not
'all'.
this is how i perform it so far:
@IncludeNumbers
VARCHAR(50)
AS
BEGIN
SELECT Phone AS [Telephone
Number]
FROM tbl_Person AS p
WHERE (p.Phone LIKE '%[' +
@IncludeNumbers + ']%')
END
(ive removed some unecessary bits but
this shows the basics)
As you can see currently it will return any phone numbers which contain any numbers given in the includeNumbers string, I would only like it to return phone numbers which contain all the numbers given in the includeNumbers string. is there any way to make it search for strings
which include 'all' numbers given
thanks
Robert

WHERE includes all letters
Bkuser
thanks
Amir Goodarzi
What you could do is to ask for numbers that has only the range in the wildcard digits, but no other digit. 'Not like not your range' will do that.
WHERE phone NOT LIKE '%[^' + @includeNumbers + ']%'
...this however, has a limitation.
If the variable contains '123', you will match against '123', '321', but not '1234', but you'll get '12' since it contains only 'allowed' digits. To correct mismatches against too short numbers, you could just filter out the shorties.
WHERE phone NOT LIKE '%[^' + @includeNumbers + ']%'
AND LEN(phone) >= LEN(@includeNumbers)
..should do what you want regarding 'exclusive' matching. It seems to work along this small example anyway =;o)
create table #x ( phone varchar(10) not null )
go
insert #x
select 123 union all
select 7717 union all
select 7727 union all
select 321 union all
select 12 union all
select 1234
go
declare @includeNumbers varchar(10)
set @includeNumbers = '123'
select * from #x
WHERE phone NOT LIKE '%[^' + @includeNumbers + ']%'
AND LEN(phone) >= LEN(@includeNumbers)
go
drop table #x
go
phone
----------
123
321
(2 row(s) affected)
=;o)
/Kenneth
-- edit --
Though not working anyway... '122' will also match, so it's not including all members of @includeNumbers anyway.. =:o/ bummer...
What you need I belive is relational division. Not sure if that is possible without breaking up each digit in @includeNumbers as it's own element, though..
Killyourface
Derek Mehlhorn - MSFT
LIKE will return anything that contains exactly the string contained in @includeNumbers, with anything you like either side of it, because you have a % on both sides. LIKE '%123%' will NOT match 7717, 7727, or 321 for that matter.
Quang Lao
set @includeNumbers = '123'
select * from #x
where phone like '%[' + @includeNumbers + ']%'
and len(@includeNumbers) = (select count(distinct pd)
from (select substring(phone, n.d, 1) as pd
from (select 1 union all select 2 union all
where substring(phone, n.d, 1) like '[' + @includeNumbers + ']'
) as t)
smnirven
Oh, OK. I've not seen that before. So you want 123 to match 321 but not 732, right I don't know of a more obvious solution than having a LIKE clause for each digit.