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

4 comments:

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

    Nemanja

    ReplyDelete
  2. 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.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete