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)

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

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter

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 Responses to "Find all columns with varchar and nvarchar datatypes"
  1. Nexa said...
    March 23, 2009 at 7:17 AM

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


  2. Hunter said...
    December 14, 2010 at 1:17 PM

    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.

  3. Noah & Sarah said...
    January 28, 2011 at 10:46 AM
    This comment has been removed by the author.
  4. Anonymous said...
    February 7, 2012 at 12:49 PM

    Type 36 = GUID


Copyright © 2009-2016 All Rights Reserved for SQLServerCurry.com by Suprotim Agarwal | Terms and Conditions