Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - Subquery (Column names)

Tags:

sql

sql-server

I need a select query thats returns the value of some columns. The column that I want starts with 'U_S'.

Select * from em

I need to transform the query above. The '*' needs to be the result (but with commas) of:

select COLUMN_NAME from information_schema.columns 
where table_name='em' and column_name like 'u_s%'
like image 238
Hugo Nunes Avatar asked Oct 20 '25 04:10

Hugo Nunes


1 Answers

There are countless examples like this, but I understand that sometimes we all need a little kick-start.

Select Stuff((Select ',' +quotename(Column_Name) 
  From information_schema.columns 
  Where table_name='em' and column_name like 'u_s%'
  For XML Path ('')),1,1,'')
like image 55
John Cappelletti Avatar answered Oct 21 '25 19:10

John Cappelletti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!