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

x
?
Solved

Convert function help

Posted on 2013-12-31
9
Medium Priority
?
345 Views
Last Modified: 2014-01-01
Experts,

Here is my code

declare @ItemIDList varchar(8000)

set @ItemIDList = '274686,275521,275522'

select * From MyDb..MyTable where ItemID in (convert(int,@ItemIDList ))

Getting the following error.
Conversion failed when converting the varchar value '274686,275521,275522' to data type int.

Could you please help me correct if I am missing something.

Thanks in Advance.
0
Comment
Question by:Tpaul_10
[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
  • 4
  • 3
9 Comments
 
LVL 46

Expert Comment

by:Kent Olsen
ID: 39749031
Hi Paul,

'274686,275521,275522' is not an integer.  What are you trying to do?


Kent
0
 

Author Comment

by:Tpaul_10
ID: 39749037
Basically I have a list of ItemIDs with comma delimited and based on the ItemIDs I need to get some information for the ItemIDs from my database table and display some information.

Hope this clarifies and please let me know if you need any further information.

Thanks
0
 

Accepted Solution

by:
Tpaul_10 earned 1000 total points
ID: 39749175
I have found a different way

create table #TmpItem (TmpItemID int)

set @startIndex = 1
   set @charIndex = charindex(',', @ItemIDList, @startIndex)
   if @charIndex = 0 and len(@ItemIDList) > 0
      set @charIndex = len(@ItemIDList) + 1
   while(@charIndex <> 0)
      begin
         insert into #TmpItem (TmpItemID)
         values (substring(@ItemIDList, @startIndex, @charIndex - @startIndex))

         set @startIndex = @charIndex + 1
         set @charIndex = charindex(',', @ItemIDList, @startIndex)
         if @charIndex = 0 and @startIndex < (len(@ItemIDList) + 1)
            set @charIndex = len(@ItemIDList) + 1
      end

select * From MyDb..MyTable where ItemID in (select TmpItemID from #TmpItem)

Thanks
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Tpaul_10
ID: 39749191
I've requested that this question be closed as follows:

Accepted answer: 0 points for Tpaul_10's comment #a39749175

for the following reason:

A
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39749189
In order to get this done you need to follow the below steps

Step 1: Create the below split function in your database

CREATE FUNCTION dbo.Split
(
 @RowData nvarchar(2000),
 @SplitOn nvarchar(5)
)
RETURNS @RtnValue table
(
 Id int identity(1,1),
 Data nvarchar(100)
)
AS
BEGIN
 Declare @Cnt int
 Set @Cnt = 1
 DECLARE @index INT
 SET @index = Charindex(@SplitOn,@RowData)
 While (@index>0)
 Begin
 Insert Into @RtnValue (data)
  Select
 Data = ltrim(rtrim(Substring(@RowData,1,@index-1)))
 
 Set @RowData = Substring(@RowData,@index+1,len(@RowData))
 Set @Cnt = @Cnt + 1
 SET @index = Charindex(@SplitOn,@RowData)
 End
 
 Insert Into @RtnValue (data)
 Select Data = ltrim(rtrim(@RowData))
 
 Return
END

Open in new window


Step 2: re-write your query as below

select * From MyDb..MyTable where ItemID in ( SELECT CAST(DATA as BIGINT) FROM dbo.split(@ItemIDList) )

Open in new window

0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39749192
There is no proper reason why this guy wants to close this question, although now we provided him a better solution.

Can you please elaborate rather than simply typing A
0
 

Author Comment

by:Tpaul_10
ID: 39749196
As I have specified I found a way to get through this issue and I have posted my code as well and your comment came in after that.

I have tested my code and is working as I am expected and that was the reason I wanted to close it.

Thanks and sorry if there is any confusion
0
 
LVL 16

Expert Comment

by:Surendra Nath
ID: 39749203
Thats Ok, no problem O.
0

Featured Post

Industry Leaders: 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

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.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

704 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