SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


convert ntext to varchar


convert ntext to varchar

Author
Message
curious_sqldba
curious_sqldba
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 3636
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 .
Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27986 Visits: 39922
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

curious_sqldba
curious_sqldba
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 3636
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
?
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31946 Visits: 18551
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Lowell
Lowell
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27986 Visits: 39922
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

--
help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

curious_sqldba
curious_sqldba
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 3636
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?
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31946 Visits: 18551
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

curious_sqldba
curious_sqldba
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2894 Visits: 3636
that is correct, i will test before making changes in production. I am trying to find most effecient and quick way if i can?
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)SSC-Dedicated (31K reputation)

Group: General Forum Members
Points: 31946 Visits: 18551
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

ricer
ricer
Valued Member
Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)Valued Member (74 reputation)

Group: General Forum Members
Points: 74 Visits: 463
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
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search