Problem with updating numbers into an NVARCHAR column

  • Hi everyone

    I'm using the following SQL in an attempt to update columns with 01, 02 etc. However, when I look into the database afterwards I seem to have 1 rather than 01, 2 rather than 02 etc.

    Could someone please let me know what I'm doing wrong?

    UPDATE UserDefFields
    SET Anl2=01
    WHEN ContactID=4435

    Many thanks
    Jon

  • Assuming Anl2 is actually a nvarchar type you need to put the 01 in single quotes '01' , what's happening is SQL Server is seeing 01 as a number so just 1 then converting that to a character.

  • Hi there

    Thank you for the quick reply. Yes, that works - as simple as that!

    Many thanks
    Jon

  • It is worth noting, storing numbers in a varchar field, if they really are numbers and not strings, can lead to other issues down the road. CAST & CONVERT functions can cause poor performance. Implicit conversions can also lead to poor performance. If it's just a string, never mind.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

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

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