Monday, 20 May 2013

How to find a column within a table

How to find a column within a table.

If like me you are working on a SQL Server data warehouse with hundreds of columns within each table, it can get really frustrating when you are trying to look for a specific column that you do not know the exact name. I wrote this little script below to help me speed up the process of writing queries. It comes in really handy when working on datasets like SUS downloads, where I do not know the exact column names.

SELECT
  *
from
  information_schema.COLUMNS
where
  column_name like '%<field name to search for>%'
  and table_name = '<table name to search>'

Just replace the <field name to search for> for the columns name that you would like to search for and replace the <table name to search>.  see example below:


SELECT
  *
from
  information_schema.COLUMNS
where
  column_name like '%Clinic%'
  and table_name = 'tbl_sus_opa_download'

Hope this helps you out; and please let me know on the comments how you get on. Thanks.

No comments:

Post a Comment