SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)

    An integer set to plave a zero in front of single numbers?

    I have a drop down menu in php that is inserting numbers into a mysql table.
    But when numbers between 1-9 are inserted I want them to add a zero in front, so that all numbers in the table are set to 2 figures.

    Guess it's simple, but just don't remember how to do it.

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    use php to append the zero -- it's a display issue, not a database issue
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    So I should do something like an if statement to put a zero in front of everything less than 10?
    Isn't there just a way to make the db save it as 2 numbers all the time?
    Think I read that sometime...

  4. #4
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    yes, do it with php

    there is a way to make the database save 2 digits, but that requires converting the column to VARCHAR, which means you won't be able to use auto_increment (if that's what you're using)
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  5. #5
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    No, I have listed a couple of numbers between 1-25.
    But I might wanna use them to add numbers to in the future, but maybe it's possible even if it's a VARCHAR?

  6. #6
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    i say again, you should really be doing this in php

    select right(concat('00',yourcolumn),2) as yourcolumn_with_leading_0_if_necessary ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  7. #7
    SitePoint Guru
    Join Date
    Nov 2004
    Location
    sweden
    Posts
    646
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If I want to put a month number into a db and be sure it's input with 2 figures. How do I do that?

    Let's say I want to input month 03 so I can use it in a 2007-03-03.
    That would be something similar I guess. Then I wouldn't add the zero in php.

  8. #8
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,270
    Mentioned
    60 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by brad62 View Post
    If I want to put a month number into a db and be sure it's input with 2 figures. How do I do that?
    as VARCHAR

    Quote Originally Posted by brad62 View Post
    Let's say I want to input month 03 so I can use it in a 2007-03-03.
    That would be something similar I guess. Then I wouldn't add the zero in php.
    you don't need it in mysql there either
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    982
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Believe Rudy, it's 100% display. You are just adding the 0 for looks, so do it in PHP.

    Code:
    $padded = sprintf("%02d", $unpadded);
    That will pad $unpadded to 2 positions, using 0's.

    Read More: http://www.php.net/manual/en/function.sprintf.php
    MySQL v5.1.58
    PHP v5.3.6


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
  •