0

am passing in a string of numbers and would like to convert this string into a list of numbers e.g.

SELECT personID from person WHERE personID IN ('927,6944')

How do I convert ('927,6944') to (927,6944)

This is the error message I get when I try and run the query:

Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '927,6944' to data type int.

crthompson
  • 15,653
  • 6
  • 58
  • 80
scot0308
  • 75
  • 1
  • 9
  • where are you generating '927,6944' from ? – Kakarot May 14 '14 at 16:15
  • From the error message, it's obviously MS SQL Server. – Pete May 14 '14 at 16:17
  • This answer may also be usable for your needs: http://stackoverflow.com/questions/18744391/t-sql-how-to-convert-comma-separated-string-of-numbers-to-integer – Pete May 14 '14 at 16:21
  • I am passing '927,6944' as a string value that is generated via java code. This value will typically be different and can contain a larger String of numbers. Yes, this is a SQL message that I get when I run the query on SQL server. – scot0308 May 14 '14 at 16:24
  • 1
    If you are passing the string to a stored procedure parameter then take a look at this post: http://stackoverflow.com/questions/19334380/how-to-split-explode-comma-delimited-string-field-into-sql-query/19335966#19335966 – bsivel May 14 '14 at 18:03
  • @podiluska, you have used your gold powers poorly. This is not about splitting a string. Please fully read and understand a question before exercising your new powers. You must be 8 times more certain that its a dupe. – crthompson May 14 '14 at 18:19
  • @paqogomez I am 20x certain it's a dupe. – podiluska May 14 '14 at 18:35
  • I was able to use the post above that bsivel provided to create a temp table that converts the values passed into the stored procedure then join on the temp table. Thanks everyone for the help. – scot0308 May 14 '14 at 18:50
  • @podiluska And now the [real answer](http://stackoverflow.com/questions/19334380/how-to-split-explode-comma-delimited-string-field-into-sql-query/19335966#19335966) (that _would_ be a dupe of this) is marked as a dupe.. by you. Great way to prove your point i suppose. – crthompson May 14 '14 at 19:56

3 Answers3

1

Remove the quotes (quotes imply varchar)

SELECT personID FROM person WHERE personID IN (927, 6944)
JasonSec
  • 614
  • 5
  • 12
1

You need to use dynamic sql. Pass the value '927,6944' as @Parameter

DECLARE @Parameter varchar(100)
DECLARE @Query varchar(200)
SET @Parameter='927,6944'   --An example

SET @Query='SELECT personID from person WHERE personID IN ('+@Parameter+')'
EXEC(@Query);

Explanation:

While executing, @Query contains the following:

SELECT personID FROM person WHERE personID IN (927, 6944)

OR

You need to make the whole query from the program itself and execute it directly.

Raging Bull
  • 18,593
  • 13
  • 50
  • 55
0

I suspect that it is possible to generate this query from your java code without the quote marks. The string with these numbers in it need not contain quote marks when being injected into the SQL. If for some reason the quote makes are not easy to remove when making the query, try passing it through a string replace method in your java code that will strip out the quote marks.

Alex Unger
  • 180
  • 2
  • 9