Tuesday, June 19, 2012

SQL Server: Text to Ntext


How to change data type text to ntext that isn’t NULL (there are records)? Since there is no way you can’t modify it, here is the alternative. In this assumption ‘kolom’ is the name of column that have to be changed and table_name is the name of table. Here you go!

1. Rename column ‘kolom’ to ‘ren-kolom’, use this code,
sp_RENAME ‘table_name.kolom’, ‘ren_kolom’, ‘COLUMN’
2. Add column ‘kolom’ data type ntext, use this code,
ALTER TABLE table_name ADD kolom ntext
3. Copy field in column ren_kolom to kolom, use this code,
UPDATE table_name SET kolom = ren_kolom
4. Delete ren_kolom, use this code,
alter table table_name drop column ren_kolom

Walla.. it changed. :)