convert ntext to varchar

  • curious_sqldba

    SSC-Dedicated

    Points: 36305

    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

    SSC Guru

    Points: 323460

    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

    SSC-Dedicated

    Points: 36305

    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

    SSC Guru

    Points: 281243

    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[/url]
    Learn Extended Events

  • Lowell

    SSC Guru

    Points: 323460

    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

    SSC-Dedicated

    Points: 36305

    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

    SSC Guru

    Points: 281243

    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[/url]
    Learn Extended Events

  • curious_sqldba

    SSC-Dedicated

    Points: 36305

    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

    SSC Guru

    Points: 281243

    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[/url]
    Learn Extended Events

  • ricer

    Right there with Babe

    Points: 722

    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

  • Evil Kraig F

    SSC Guru

    Points: 100851

    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?

    An nText -> nVarchar conversion is not going to save you any space. It's a good idea to get away from the old LOB formats, as nVarchar(max) is much easier to work with, however.

    nVARCHAR(MAX) will still act like LOB data, using independent data pages to store the overflow data. You will probably want to setup a separate filegroup for the LOB data, as it's less painful to administrate that way and you don't end up with as many fragmentation issues you do if you leave it in the primary filegroup. A second file on the same drive will still make your life easier here.

    You still want to do the conversion, but not for space reasons.

    Something you may want to do, because of the size, and if you have the room, is rebuild the table as a second table and swap them, so you don't have to lock the database up long term. You'd need auditing for something like that (even just store the IDs of new/updated/deleted records via trigger) while it builds, so that between the swaps you can refresh any rows that were modified between when you last read it and what it was just before you removed it from availability.


    - Craig Farrell

    Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.

    For better assistance in answering your questions[/url] | Forum Netiquette
    For index/tuning help, follow these directions.[/url] |Tally Tables[/url]

    Twitter: @AnyWayDBA

  • curious_sqldba

    SSC-Dedicated

    Points: 36305

    ricer (6/10/2011)


    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.

    Yeah i might go this route. Thanks

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic. Login to reply