June 16, 2008

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, ASP.NET Architecture MVP works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls.

Follow him on twitter @suprotimagarwal

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 & Sarah said...
This comment has been removed by the author.
Anonymous said...

Type 36 = GUID