sql select with column name like

This will get you the list

select * from information_schema.columns 
where table_name="table1" and column_name like 'a%'

If you want to use that to construct a query, you could do something like this:

declare @sql nvarchar(max)
set @sql="select "
select @sql = @sql + '[' + column_name +'],'
from information_schema.columns 
where table_name="table1" and column_name like 'a%'
set @sql = left(@sql,len(@sql)-1) -- remove trailing comma
set @sql = @sql + ' from table1'
exec sp_executesql @sql

Note that the above is written for SQL Server.

Leave a Comment