HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How to select column names by data type

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnnamestypehowselectdata

Problem

How would I get the names of all the columns in a table that are of a specific type such as datetime?

Even better -- How can I do the same thing but with multiple tables joined together and then list the column name along with the table it comes from?

Solution

Query sys.columns and sys.types like this:

select object_name(c.object_id) TableName, * 
FROM sys.columns c
join sys.types t ON c.system_type_id = t.system_type_id
where t.name = 'datetime'

Code Snippets

select object_name(c.object_id) TableName, * 
FROM sys.columns c
join sys.types t ON c.system_type_id = t.system_type_id
where t.name = 'datetime'

Context

StackExchange Database Administrators Q#140756, answer score: 7

Revisions (0)

No revisions yet.