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)
 simple find string RESOLVED

Author  Topic 

Trent
Starting Member

39 Posts

Posted - 2006-05-10 : 15:01:08
I want to find out if a string exists in another string.
It should only be true if it matches exactly, I've tried charindex, but it doesn't quite work right.
Example:
select charindex('1', '1,20') = 1 *this is CORRECT
select charindex('1', '2,14') = 3 *this is INCORRECT, it should be false since '1' is just part of '14' rather than the exact number.

Thanks for any help!

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2006-05-10 : 16:21:08
How about this:

IF CHARINDEX('1,', '2,14') > 0 OR RIGHT('2,14', 2) = ',1'
BEGIN
...

The first part looks for instances of '1' followed by a comma. The second part checks if '1' is the last number in the string (this assumes that the numbers are all delimited by commas without spaces in between).
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2006-05-10 : 16:22:20
select charindex(',1,', ',2,14,')
or for a variable
select charindex(',1,', ',' + @var + ',')


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

Trent
Starting Member

39 Posts

Posted - 2006-05-10 : 16:27:29
That'll work, thanks.
Go to Top of Page
   

- Advertisement -