Conversion failed when converting the varchar value to data type int.

Posted on 2009-04-01
Last Modified: 2012-05-06
I have a web application that defines the list of order numbers that I want to update in a DB. This list of order numbers (which is defined as int in the DB) is collected in a string. I would like to pass the value of this string to the sql parameter to update the DB, but when I run it I get the error message: "Conversion failed when converting the varchar value ''44','45'' to data type int."

This code below represents the situation:
declare @OrderSelection varchar(max)
set @OrderSelection = '''44'',''45'''
update SY1_OrderDetail set OrderLineStatus = 1
where orderNumber in (@OrderSelection)

Please, could you help me to solve the problem?
declare @OrderSelection varchar(max)
set @OrderSelection = '''44'',''45'''
update SY1_OrderDetail set OrderLineStatus = 1
where orderNumber in (@OrderSelection)

Open in new window

Question by:EdsSystem
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
LVL 37

Expert Comment

ID: 24040263
that won't work
is this a one time thing or do you need to run this in a  procedure>?

Author Comment

ID: 24040294
I will use it in a procedure.
LVL 39

Accepted Solution

BrandonGalderisi earned 500 total points
ID: 24040316
You have to put it in dynamic SQL in order to pass @OrderSelection in like that.  Or, use a UDF to split the @OrderSelection into a table and join to it or us in/exists.

Here's the function I wrote to do it:

--with dynamic sql
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
exec('update SY1_OrderDetail set OrderLineStatus = 1 where orderNumber in (' + @OrderSelection + ')')
--with function 1
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
update SY1_OrderDetail set OrderLineStatus = 1 where orderNumber in
(select cast(theValue as int) from dbo.fn_delimitedToString(@OrderSelection,','))
--with function 2
declare @OrderSelection varchar(max)
set @OrderSelection = '44,45'
update sy 
set OrderLineStatus = 1 
from SY1_OrderDetail sy
where exists
(select 1 from dbo.fn_delimitedToString(@OrderSelection,',') where cast(theValue as int) = sy.OrderNumber)

Open in new window


Author Closing Comment

ID: 31565353
Thanks a lot for your prompt response!!!

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SqlServer Table Triggers 3 28
Rebooting Witness SQL Server 2 25
Returning Message/Value Back to Access from a SQL Server trigger 3 34
Related to SQL Query 5 21
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
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.

756 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