SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: Data type for number beginning with 0?

  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    stirling
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Data type for number beginning with 0?

    Hi, I need to know if there is a datatype in SQL that will allow me to store a number like 01234 without removing the 0 from the start.
    I have tried INTEGER AND VARCHAR but they both take the 0 away.
    Thanks.

  2. #2
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Integer is definetly not what you want, that will store the number in int value and remove any leading zeros.
    Varchar is a variable character map and will use only what is necessary, so 01234 will only store 1234 in the same way will 0000001234; this is to save your database size.

    Use CHAR() field which is fixed and will store your 01234 exactly

    Cheers
    ~ Daniel Macedo

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    stirling
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but that didnt work unfortunately, did the same as before. Could it be something to do with the fact that i am using a MySQL server and its syntax may be different?

  4. #4
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am refering to MySQL.

    Check your script, if your are storing the number as int it will not work, use a string:

    PHP Code:
    <?
    $number 
    01234// is wrong
    $number '01234'// will work
    ?>
    ~ Daniel Macedo

  5. #5
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,462
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    VARCHAR will store exactly what you give it, it will not strip the zeroes

    you could use INTEGER(5) UNSIGNED ZEROFILL

    but if you're not going to add these numbers up, or find their averages, which is unlikely if you want these numbers used like a product code or something, then i would use VARCHAR
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Evangelist DMacedo's Avatar
    Join Date
    May 2004
    Location
    Braga, Portugal
    Posts
    596
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    I had the idea VARCHAR would strip the leading zeroes, which he confirmed, so maybe it's his script that sends SQL an INT type.

    I did think of setting UNSIGNED ZEROFILL to mysql INT type, but filling with leading zeroes if the number is smaller seems relevant in his case.
    Either varchar or char would work.
    ~ Daniel Macedo

  7. #7
    SitePoint Guru
    Join Date
    Jan 2004
    Location
    Uppsala, sverige
    Posts
    690
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    VARCHAR will store exactly what you give it.
    No, Mysql removes trailing spaces when insert into varchar columns.

  8. #8
    SQL Consultant silver trophybronze trophy
    SitePoint Award Recipient r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    38,462
    Mentioned
    35 Post(s)
    Tagged
    1 Thread(s)
    the rest of the quote was "it will not strip the zeroes"

    i was referring to the stripping of the zeroes, VARCHAR will store exactly all zeroes you give it

    but of course you are right, blanks do get stripped
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2004
    Location
    stirling
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but i got it now. It was my script. Never changed the string to VARCHAR when i changed the type. Thanks all. I am enlightened! dh.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •