Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

CHARINDEX Chooses wrong result

Posted on 2011-03-21
3
Medium Priority
?
390 Views
Last Modified: 2012-06-21
CHARINDEX works most of the time very well, but sometimes it chooses the wrong id. ID is an integer while the search string is varchar      

DECLARE @MyId AS VARCHAR (500)
      SET @MyId = '7,17,107,177'
      
SELECT * FROM leads.BatchDetails
WHERE
  (@MyId IS NULL OR @MyId = '' OR
             CHARINDEX(CAST(Id AS VARCHAR),@MyId)>0)

This will select from the DB '10' because it finds it in '107'
7
10
17
107
177

The problem is id 10


0
Comment
Question by:Internet_Engineer
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
ssisworo earned 2000 total points
ID: 35182697
try this :
and always add ',' at end of @MyId

---------------------
DECLARE @MyId AS VARCHAR (500)
      SET @MyId = '7,17,107,177,'
     
SELECT CHARINDEX(cast(Id as varchar), @MyId ), * FROM Person
WHERE
  @MyId IS NULL OR @MyId = '' OR
             ( CHARINDEX(cast(Id as varchar), @MyId )>0 and
               substring(@MyId,CHARINDEX(cast(Id as varchar), @MyId ) + len(cast(Id as varchar)),1) = ',')

--------------------
0
 
LVL 3

Expert Comment

by:ssisworo
ID: 35182781

DECLARE @MyId AS VARCHAR (500)
      SET @MyId = '7,17,107,177,'
     
SELECT * FROM Personleads.BatchDetails
WHERE
  @MyId IS NULL OR @MyId = '' OR
             ( CHARINDEX(cast(Id as varchar), @MyId )>0 and
               substring(@MyId,CHARINDEX(cast(Id as varchar), @MyId ) + len(cast(Id as varchar)),1) = ',')
0
 

Author Closing Comment

by:Internet_Engineer
ID: 35182829
It works
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question