Selecting from INFORMATION_SCHEMA.COLUMNS is a good way to determine what columns need to be converted,
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE DATA_TYPE = 'CHAR';
But it can't be used to directly change a column's data type. ALTER TABLE is used to modify column data types:
ALTER TABLE [dbo].[TableName] ALTER COLUMN [ColumnName] NVARCHAR(50);
While you're at it, avoid using NVARCHAR(MAX)
unless it's absolutely necessary. Make sure your data types are sized specifically for the attribute. If your CHAR
columns are already sized correctly, use the following script to generate the ALTER TABLE
statements:
SELECT 'ALTER TABLE ' +
QUOTENAME(TABLE_SCHEMA) + '.' +
QUOTENAME(TABLE_NAME) +
' ALTER COLUMN ' +
QUOTENAME(COLUMN_NAME) +
' NVARCHAR(' +
CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(4)) + ');'
FROM INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'char';
Keep in mind that this only generates the ALTER TABLE statements, you'll need to copy the results and execute in a new tab to change the data types.