Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all columns of type nvarchar(max) in all tables in a SQL Server database

Tags:

sql

sql-server

I found this question on stackoverflow that almost answers my question: Find all columns of a certain type in all tables in a SQL Server database

However I need to find all fields of type nvarchar(max) specifically. If I try this:

SELECT table_name [Table Name], column_name [Column Name]
FROM information_schema.columns where data_type = 'nvarchar(max)'

It doesn't work and nothing is returned. If I try this:

SELECT table_name [Table Name], column_name [Column Name]
FROM information_schema.columns where data_type = 'nvarchar'

It works but there are hundreds of results and I only care about fields of max size. How do I select all nvarchar(max) fields specifically?

like image 679
Legion Avatar asked Feb 22 '17 21:02

Legion


People also ask

How do I find the datatype of all columns in SQL?

You can get the MySQL table columns data type with the help of “information_schema. columns”. SELECT DATA_TYPE from INFORMATION_SCHEMA. COLUMNS where table_schema = 'yourDatabaseName' and table_name = 'yourTableName'.

How do I find LOB columns in SQL Server?

Bookmark this question. Show activity on this post. Looking for a script to scan all tables in all SQL Server databases and list the columns which are large objects (TEXT, NTEXT, ,IMAGE VARCHAR(MAX), NVARCHAR(MAX), FILESTREAM, XML, VARBINARY).

Can you index nvarchar Max?

Columns with data types like nvarchar(max), text, and ntext cannot be used in indexes.


1 Answers

The character_maximum_length will be -1 for max.

select 
    table_name as [Table Name]
  , column_name as [Column Name]
from information_schema.columns 
where data_type = 'nvarchar' 
  and character_maximum_length=-1
like image 65
SqlZim Avatar answered Oct 06 '22 14:10

SqlZim