Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Matching a table of prefixes

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-22 : 10:08:58
Aron writes "Hya,

I have a table full of prefixes in one field. I need to match a string against that list of prefixes and report if it matches any. Is it possible to do this using the LIKE function or is there any way short of iterating through the entire table?

Example:

tblPrefixes contains:
44551
44552
44553
44554

Query:
Does '4455212345' match any of the prefixes?


Thanks,
Aron."

Nazim
A custom title

1408 Posts

Posted - 2002-01-22 : 10:38:51
if i have understood your requirements correctly, this should do what you are looking for


select * from tblPrefixes where customerkey like substring('4455212345',1,len(customerkey))

--------------------------------------------------------------
Dont Tell God how big your Problem is , Tell the Problem how Big your God is
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-01-22 : 12:01:23
select *
from tblPrefixes
where '4455212345' like prefix + '%'


select * from tblPrefixes where customerkey like substring('4455212345',1,len(customerkey))
doesn't have any wildcards so should be an = instead of like.
Be careful of this because there are (or were) bugs in v2000 regarding like without wildcards.


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

arjs
Starting Member

1 Post

Posted - 2002-01-23 : 04:58:48
I ended up with ... is there anything wrong with this?

select *
from tblPrefixes
where CHARINDEX(customerkey, '4455212345') = 1


Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2002-01-23 : 05:39:28
Nope should do the same as the others.

==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -