Find all columns with varchar and nvarchar datatypes

If you need to find out all the columns with the varchar and nvarchar datatypes, use this query.

-- List all columns with varchar and nvarchar data type
SELECT OBJECT_NAME(col.OBJECT_ID) as [TableName], col.[name] as [ColName], typ.[name]
FROM sys.all_columns col
INNER JOIN sys.types typ
ON col.user_type_id = typ.user_type_id
WHERE col.user_type_id IN (167,231)
GO

Similarly you can use other usertypeid as shown below to find other datatypes.

bigint 127
binary 173
bit 104
char 175
datetime 61
decimal 106
float 62
image 34
int 56
money 60
nchar 239
ntext 99
numeric 108
nvarchar 231
real 59
smalldatetime 58
smallint 52
smallmoney 122
sql_variant 98
sysname 256
text 35
timestamp 189
tinyint 48
uniqueidentifier 36
varbinary 165
varchar 167
xml 241


About The Author

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and a new one recently at The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigous Microsoft MVP award for nine times in a row now. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that represents premium web sites and digital publications comprising of Professional web, windows, mobile and cloud developers, technical managers, and architects.

Get in touch with him on Twitter @suprotimagarwal, LinkedIn or befriend him on Facebook

4 comments:

Nexa said...

Excellent query. Thank you a lot. It was very helpful and saved me a lot of time.
All the best.

Nemanja

Hunter said...

Thanks for this. The sys.* tables are a very powerful way to get at the structure of database tables and columns, but it's frequently difficult to figure out just how to get what you're looking for.

Noah Meyer said...
This comment has been removed by the author.
Anonymous said...

Type 36 = GUID