Solved

CHARINDEX Chooses wrong result

Posted on 2011-03-21
3
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
ssisworo earned 500 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

734 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