Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

CHARINDEX Chooses wrong result

Posted on 2011-03-21
3
Medium Priority
?
389 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 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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

721 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