Search code examples
sql-serverselectsql-like

SQL Server Select Where value LIKE(temporary table value)


I have a function in SQL server 2008 that takes a string: 'A,B,C,D' and splits it and creates a table of the values.

Values
------
A
B
C
D

I now want to search a table (Users) Where a column value is LIKE one of the rows (surname) in the above table.

This is what I would like to do:

SELECT * FROM Users WHERE vLastName LIKE 'A%'
SELECT * FROM Users WHERE vLastName LIKE 'B%'
SELECT * FROM Users WHERE vLastName LIKE 'C%'
SELECT * FROM Users WHERE vLastName LIKE 'D%'

If the above is not possible, how else would you do it? Some kind of loop?

I'm using SQL Server 2008


Solution

  • SELECT * FROM Users,NewTable WHERE vLastName LIKE Values + '%'