SQL Server Developer Center > SQL Server Forums > SQL Server XML > XQuery: Error converting data type nvarchar to numeric.
Ask a questionAsk a question
 

AnswerXQuery: Error converting data type nvarchar to numeric.

  • Thursday, August 09, 2007 6:27 PMlcj Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I am trying to run a query off of a xml data type using Xquery (nodes() and value() methods) but am running inot a conversion error for a numeric type in the xml which is empty or blank.  I want it to come back as null but it looks like it is trying to convert an empty string.  Is there any way that an empty tag could pull back a null?

Answers

  • Thursday, August 16, 2007 12:29 PMKent Waldrop _ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I am not sure that I understand the problem; however you might be able to use the TSQL NULLIF function to mold the data as you need it.  Perhaps something like this:

     

    Code Snippet

    declare @xml xml
    set @xml =
    '<Root>
       <Data></Data>
       <Data>17</Data>
    </Root>'

     

    select cast(nullif(t.value('.', 'varchar(11)'),'') as int)
           as targetValue
      from @xml.nodes('/Root/Data') as x(t)

     

    /*
    targetValue
    -----------
    NULL
    17
    */

     

     

All Replies

  • Friday, August 10, 2007 1:51 PMMartin HonnenModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Can you show us an example of your XML and of your query? Then it is easier to understand what you are trying to achieve and where it fails.

     

  • Thursday, August 16, 2007 12:29 PMKent Waldrop _ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer

    I am not sure that I understand the problem; however you might be able to use the TSQL NULLIF function to mold the data as you need it.  Perhaps something like this:

     

    Code Snippet

    declare @xml xml
    set @xml =
    '<Root>
       <Data></Data>
       <Data>17</Data>
    </Root>'

     

    select cast(nullif(t.value('.', 'varchar(11)'),'') as int)
           as targetValue
      from @xml.nodes('/Root/Data') as x(t)

     

    /*
    targetValue
    -----------
    NULL
    17
    */

     

     

  • Friday, January 23, 2009 12:54 PMusm121 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Although an old post , but just to help others

    a simpler way of doing the above is

     

    declare @xml xml

    set @xml =

    '<Root>

    <Data></Data>

    <Data>17</Data>

    </Root>'

     

     

    select t.value('(./text())[1]', 'decimal')

     

    as targetValue

    from @xml.nodes('/Root/Data') as x(t)


    usm121
  • Thursday, November 05, 2009 7:54 AMsahe Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    hi, how this can be achieved while retreiving attribute value
  • Thursday, November 05, 2009 1:14 PMwBobAnswererUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Please post a small piece of sample XML and your expected results.