Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

convert ntext to varchar Expand / Collapse
Author
Message
Posted Thursday, June 9, 2011 2:23 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 PM
Points: 1,274, Visits: 2,936

can someone please advice me on how to convert ntext data type to varchar? I think ntext data type is stored on a different page .
Post #1123015
Posted Thursday, June 9, 2011 2:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:22 PM
Points: 12,889, Visits: 31,839
here's the full reference to Cast And Convert; it's good to read, it's something you end up using a lot
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Select 
convert(nvarchar(max),TheNTextColumn) AsColumnName
From TableName



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1123028
Posted Thursday, June 9, 2011 3:05 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 PM
Points: 1,274, Visits: 2,936
Lowell (6/9/2011)
here's the full reference to Cast And Convert; it's good to read, it's something you end up using a lot
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Select 
convert(nvarchar(max),TheNTextColumn) AsColumnName
From TableName



I need to convert and replace with varchar data type. Dont i need to run an update statement after i change?


update dbo.mytable set mycolumn = mycolumn
?
Post #1123038
Posted Thursday, June 9, 2011 3:09 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:01 PM
Points: 21,291, Visits: 14,984
sqldba_icon (6/9/2011)
Lowell (6/9/2011)
here's the full reference to Cast And Convert; it's good to read, it's something you end up using a lot
http://msdn.microsoft.com/en-us/library/ms187928.aspx

Select 
convert(nvarchar(max),TheNTextColumn) AsColumnName
From TableName



I need to convert and replace with varchar data type. Dont i need to run an update statement after i change?


update dbo.mytable set mycolumn = mycolumn
?


If you are changing the column data type at the table definition, then no you will not need to run an update statement after you change the datatype.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1123042
Posted Thursday, June 9, 2011 3:15 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:22 PM
Points: 12,889, Visits: 31,839
ok, i think you can simply alter the column.
ALTER TABLE MyTable ALTER COLUMN MyNTextColumn VARCHAR(MAX)

let us know if that fails, it worked on a simple test table i created...but if you have data that was bigger than 4000 chars, i'm wondering if hte process requires adding the new column, moving the data and dropping the previous.....


Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1123043
Posted Thursday, June 9, 2011 3:55 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 PM
Points: 1,274, Visits: 2,936

thanks..let me provide some more details..there is a table more than 100 gb in size with a column ntext which occupies 90% of total table space. Goal is to change data type to varchar and shrink the db..Any ideas?
Post #1123061
Posted Thursday, June 9, 2011 4:10 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:01 PM
Points: 21,291, Visits: 14,984
sqldba_icon (6/9/2011)

thanks..let me provide some more details..there is a table more than 100 gb in size with a column ntext which occupies 90% of total table space. Goal is to change data type to varchar and shrink the db..Any ideas?


Does the table contain Unicode characters? You need to ensure that you will not encounter data problems by converting to varchar.

I would copy the entire table to a new table and then test the conversion on that new table. Do it a few times to ensure it will work as desired.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1123066
Posted Thursday, June 9, 2011 9:58 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, July 21, 2014 3:43 PM
Points: 1,274, Visits: 2,936

that is correct, i will test before making changes in production. I am trying to find most effecient and quick way if i can?
Post #1123116
Posted Friday, June 10, 2011 2:57 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:01 PM
Points: 21,291, Visits: 14,984
Since it contains unicode characters, then convert it to nvarchar instead of varchar.

As for the best way to do it -after doing it on the backup table a few times, then update the actual table. Always make sure you have a good backup of the database first.




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1123679
Posted Friday, June 10, 2011 4:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Tuesday, December 3, 2013 6:05 PM
Points: 42, Visits: 410
If you are sure it won't truncate any data then I would create a new table with the nvarchar column. Copy all the data from old table to new table make sure the records match up. Rename old table to a different name and then rename new table to old table name. Truncate old table and drop it.



-------------------------------------------------
Will C,
MCITP 2008 Database Admin, Developer
Post #1123701
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse