SQL 2008 - why Integer Column trims Leading Zeros

  • I need to have a Leading Zero's in an Integer column. Is there a way to do it?

    i.e. I require 00020 to be stored in table as 00020 and not as 20

  • Then it's a string, not an integer.

    Store it as an integer, and handle how you display that later. Perhaps as a computed column?

    Rob Farley
    LobsterPot Solutions & Adelaide SQL Server User Group
    Company: http://www.lobsterpot.com.au
    Blog: http://blogs.lobsterpot.com.au

  • To illustrate Rob's point:

    DECLARE @Sample

    TABLE (

    raw_data INTEGER NOT NULL,

    fixed_length AS

    RIGHT(100000 + raw_data, 5)

    );

    INSERT @Sample (raw_data) VALUES (20);

    SELECT raw_data, fixed_length

    FROM @Sample;

    Output:

    raw_data fixed_length

    ======== ============

    20 00020

  • Thanks SQLKiwi, that was really helpful...

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

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