0

I have query like below

select * from table1 where id in (select ids from table2 where id = 100)

The subquery returns me output like 34,35,55,66 if i run it individually.

But my above query gives error as

Conversion failed when converting the varchar value '34,35,55,56' to data type int.

The id column in table1 is int. I am aware that i can do this in 2 seperate queries but if there is any way to do in single query then please let me know. Many thanks in advance

9
  • 1
    Please don't store multiple values in one column. That is a real pain like in your case. You should really change your DB structure. Commented Jun 24, 2013 at 13:05
  • @juergend no i cant change as its existing database. Commented Jun 24, 2013 at 13:06
  • Even an existing DB can be changed... Please think about doing it even if you have to change a few things. Commented Jun 24, 2013 at 13:08
  • 2
    There is a logical difference between a single string that happens to contain sets of digits and commas, and multiple integers. This is as true in SQL as it is in practically any other language I can think of. It really would be better to change the structure, because you'll continue to hit pain points with this current design. If you insist on continuing, you need to look up splitting strings on commas - which has been asked and answered plenty of times on here. Commented Jun 24, 2013 at 13:09
  • @Damien_The_Unbeliever can you give me link where we can split string in commas Commented Jun 24, 2013 at 13:12

1 Answer 1

1

I don't know about sql server and the comments about this kind of structure being undesirable notwithstanding, you can do it in mysql with the following, per this fiddle.

select distinct id from table1 join table2 on find_in_set(id,ids)

Update: I just realized this doesn't reflect table2 having it's own id field and restricting the search based on that. If you need help modifying the query to accomodate let me know and I'll update it and the fiddle.

Update 2: Here's a lame but perhaps valid attempt to do this in SQL Server with its much more limited string facilities, per this fiddle. If anyone knows of a simpler find_in_set equivalent for SQL Server, I'd be curious to know what it is.

select distinct id from table1 join table2 
  on patindex('%,'+cast(id as varchar)+',%',ids)>0 or
  patindex(cast(id as varchar)+',%',ids)>0 or
  patindex('%,'+cast(id as varchar),ids)>0 or
  patindex(cast(id as varchar),ids)>0
Sign up to request clarification or add additional context in comments.

4 Comments

Looks like you can use msdn.microsoft.com/en-us/library/ms188395.aspx in lieu of find_in_set
Is it possible for you to convert this to sql?
I give you +1 for your efforts. But just leave it now because i dont think its possible with single query. I give up :(
@AshReva: The update I made to my answer 16 hours ago works in SQL Server per the fiddle link I included. Were you looking for some other version of SQL?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.