locked
how to convert column value nvarchar(MAX) to nchar(10) without loss data? RRS feed

  • Question

  • how to convert column value nvarchar(MAX) to nchar(10) without loss data?

     

     


     

     

     

     

     


    • Edited by srajmuneer Saturday, December 3, 2011 1:18 PM
    Friday, December 2, 2011 3:25 AM

Answers

  • Your title doesn't match your question.  This appears to be a different question that your title suggest and different than your last question that was supposedly a duplicate.

    If you're converting from nvarchar(MAX) to nchar(10) you're very likely to have data loss for any data over 10 characters long.  You'll also have padded spaces at the end of your resulting column if the nvarchar(max) data is less than 10 characters long.  NCHAR(10) is a fixed width column of 10 characters.  NVARCHAR(MAX) is a theoritically unlimited number of columns of data of varying length. 

    The only way you can convert the data without loss is to select and convert only the data that is less than or equal to 10 characters and leave the other data alone.

    SELECT CAST(nvarcharmaxcolumnname AS NCHAR(10)) FROM tablename WHERE LEN(nvarcharmaxcolumnname) <= 10

     

     

     


    Eric Isaacs
    • Edited by Eric Isaacs Saturday, December 3, 2011 1:15 AM
    • Proposed as answer by Naomi NEditor Sunday, December 4, 2011 7:09 AM
    • Marked as answer by srajmuneer Sunday, December 4, 2011 2:41 PM
    Saturday, December 3, 2011 1:14 AM

All replies

  • Please do not put duplicate posts.

    Your question is answered in TSQL forum: http://social.msdn.microsoft.com/Forums/en-US/transactsql/thread/a378fd64-20e4-4c01-bb4b-f9e1798a4b0a


    ~manoj | email: http://scr.im/m22g
    http://sqlwithmanoj.wordpress.com
    MCCA 2011
    • Proposed as answer by Eric Isaacs Saturday, December 3, 2011 1:07 AM
    • Unproposed as answer by Eric Isaacs Saturday, December 3, 2011 1:08 AM
    Friday, December 2, 2011 5:36 AM
  • Your title doesn't match your question.  This appears to be a different question that your title suggest and different than your last question that was supposedly a duplicate.

    If you're converting from nvarchar(MAX) to nchar(10) you're very likely to have data loss for any data over 10 characters long.  You'll also have padded spaces at the end of your resulting column if the nvarchar(max) data is less than 10 characters long.  NCHAR(10) is a fixed width column of 10 characters.  NVARCHAR(MAX) is a theoritically unlimited number of columns of data of varying length. 

    The only way you can convert the data without loss is to select and convert only the data that is less than or equal to 10 characters and leave the other data alone.

    SELECT CAST(nvarcharmaxcolumnname AS NCHAR(10)) FROM tablename WHERE LEN(nvarcharmaxcolumnname) <= 10

     

     

     


    Eric Isaacs
    • Edited by Eric Isaacs Saturday, December 3, 2011 1:15 AM
    • Proposed as answer by Naomi NEditor Sunday, December 4, 2011 7:09 AM
    • Marked as answer by srajmuneer Sunday, December 4, 2011 2:41 PM
    Saturday, December 3, 2011 1:14 AM