all groups > sql server programming > november 2003 >
You're in the

sql server programming

group:

Fast way to change all NVarChar types to Varchar?



Fast way to change all NVarChar types to Varchar? John Rugo
11/15/2003 10:22:54 PM
sql server programming: Hi All,
Is there a prefered method of changing a bunch of fields from NVarChar to
VarChar?

Re: Fast way to change all NVarChar types to Varchar? Aaron Bertrand [MVP]
11/15/2003 10:50:31 PM
Do you mean aside from the ALTER TABLE statement?



[quoted text, click to view]

Re: Fast way to change all NVarChar types to Varchar? John Rugo
11/16/2003 8:36:15 AM
yes. Somthing to seak out every table and row definition and automatically
change the data type from NVarChar to VarChar.

[quoted text, click to view]

Re: Fast way to change all NVarChar types to Varchar? Louis Davidson
11/16/2003 10:33:41 PM
Well, technically the preferred method is to get the datatype right the
first time :)

This code may help. Here is a sample table:

drop table testTheory
go
create table testTheory
(
id int,
value nvarchar(10) not null,
value2 nvarchar(100) null,
)
go

--this will build a script to find nvarchar columns and change them to
varchar ones.
select 'alter table [' + table_name + '] alter column [' + column_name + ']
varchar('+ cast(character_maximum_length as varchar(10)) + ')' +
case when is_nullable = 'Yes' then ' NULL ' else ' NOT NULL ' end
from information_schema.columns
where data_type = 'nvarchar'
go

--this was the output
alter table [testTheory] alter column [value] varchar(10) NOT NULL
alter table [testTheory] alter column [value2] varchar(100) NULL

--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

Re: Fast way to change all NVarChar types to Varchar? John Rugo
11/17/2003 3:03:29 PM
Yes I know. I had build this with international support in mind; but that
has changed, and I wanted to optimize as much a spossible.

Thanks

[quoted text, click to view]

Re: Fast way to change all NVarChar types to Varchar? Louis Davidson
11/17/2003 6:15:31 PM
Hey, I was just kidding. I know these things happen :) All designs are
fluid throughout time!

--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management

Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266

Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)

[quoted text, click to view]

AddThis Social Bookmark Button