I am currently trying to convert all columns in all tables of my database from char to nvarchar in SQL server 2012.
the query I am trying to run is
select cast((select COLUMN_NAME
from INFORMATION_SCHEMA.COLUMNS
where DATA_TYPE = 'char') as nvarchar(MAX))
Not sure if I should be changing the data type through INFORMATION_SCHEMA.COLUMNS but I found this to be the best way to grab the datatype.
the error I am currently getting is:
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Is there a better way to accomplish this or a way around this error, as it seems it doesn't like me changing datatypes to multiple rows at the same time.
Selecting from INFORMATION_SCHEMA.COLUMNS is a good way to determine what columns need to be converted,
But it can't be used to directly change a column's data type. ALTER TABLE is used to modify column data types:
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 yourCHAR
columns are already sized correctly, use the following script to generate theALTER TABLE
statements: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.