Home > SQL Server Stuff > SQL IN operator string builder

SQL IN operator string builder

December 11th, 2007

Have you ever had a list in excel, that you need to query from a database? Something like this:

abc

def

ghi

jkl

mno

Then you had to turn it into a SQL query like this:

Select * from mytable where some_value in (’abc’,'def’,'ghi’,'jkl’,'mno’)

Yeah…not so bad when its four or five…or you don’t work on a trading floor where a trader is down your neck wanting an answer immediately. Well I solved the problem. The following script will turn a comma/space/custom delimited list into a perfect string you can use in SQL.

If you’d like to run this on your own server or edit it. Contact me and I will gladly send you the source code.

http://www.gregjessup.com/scripts/SQL/

Greg SQL Server Stuff

  1. No comments yet.
  1. No trackbacks yet.