Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. mysql query to insert 0's

mysql query to insert 0's

Scheduled Pinned Locked Moved Database
databasemysqltutorial
13 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A AndyInUK

    Hi, I have a unique id column in a table with values - 12 32 45 345 3534 45456 etc. Now i want to make all the values to be 7 digit by placing 0's in front of each id for example above ids will be 0000012 0000032 0000045 0000345 0003534 0045456 Now there are 1000's of values that i need to change - what would be the best possible way to write query for this. Thanks

    D Offline
    D Offline
    David Skelly
    wrote on last edited by
    #2

    LPAD(my_col, 7, '0')

    1 Reply Last reply
    0
    • A AndyInUK

      Hi, I have a unique id column in a table with values - 12 32 45 345 3534 45456 etc. Now i want to make all the values to be 7 digit by placing 0's in front of each id for example above ids will be 0000012 0000032 0000045 0000345 0003534 0045456 Now there are 1000's of values that i need to change - what would be the best possible way to write query for this. Thanks

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #3

      That would only work if they're string values, which they shouldn't be. You could left pad them when you display them.

      A D 2 Replies Last reply
      0
      • P PIEBALDconsult

        That would only work if they're string values, which they shouldn't be. You could left pad them when you display them.

        A Offline
        A Offline
        AndyInUK
        wrote on last edited by
        #4

        yeh they are int values. Anyway to do it for int ? thanks

        P 1 Reply Last reply
        0
        • A AndyInUK

          yeh they are int values. Anyway to do it for int ? thanks

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #5

          AndyInUK wrote:

          yeh they are int values. Anyway to do it for int ?

          Why would you want to? The following values are all the same: 1 01 001 0001 Generally, you'll apply padding purely for display/writing to file purposes, and this should be accomplished in the client code - not in the database.

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          E 1 Reply Last reply
          0
          • P PIEBALDconsult

            That would only work if they're string values, which they shouldn't be. You could left pad them when you display them.

            D Offline
            D Offline
            David Skelly
            wrote on last edited by
            #6

            True enough. I assumed they were strings because the question doesn't really make sense if they are numeric.

            1 Reply Last reply
            0
            • P Pete OHanlon

              AndyInUK wrote:

              yeh they are int values. Anyway to do it for int ?

              Why would you want to? The following values are all the same: 1 01 001 0001 Generally, you'll apply padding purely for display/writing to file purposes, and this should be accomplished in the client code - not in the database.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              E Offline
              E Offline
              eraser950
              wrote on last edited by
              #7

              yes they mean same but when we try to sort its meaning will change thats y for sorting we need this

              D 1 Reply Last reply
              0
              • E eraser950

                yes they mean same but when we try to sort its meaning will change thats y for sorting we need this

                D Offline
                D Offline
                dan sh
                wrote on last edited by
                #8

                Can you explain? Why do you need to have preceding zeroes in a number to sort? Simply put, if it is an integer, then 1. you cannot do it 2. you do not need it

                E 1 Reply Last reply
                0
                • D dan sh

                  Can you explain? Why do you need to have preceding zeroes in a number to sort? Simply put, if it is an integer, then 1. you cannot do it 2. you do not need it

                  E Offline
                  E Offline
                  eraser950
                  wrote on last edited by
                  #9

                  because without zeroes its not sorting in ascending like i have data 1 2 3 10 11 21 31 without zeroes its sorting like this 1 11 21 31 2 3

                  D M P 3 Replies Last reply
                  0
                  • E eraser950

                    because without zeroes its not sorting in ascending like i have data 1 2 3 10 11 21 31 without zeroes its sorting like this 1 11 21 31 2 3

                    D Offline
                    D Offline
                    dan sh
                    wrote on last edited by
                    #10

                    Bullshit if these values are in integer column.

                    1 Reply Last reply
                    0
                    • E eraser950

                      because without zeroes its not sorting in ascending like i have data 1 2 3 10 11 21 31 without zeroes its sorting like this 1 11 21 31 2 3

                      M Offline
                      M Offline
                      Mycroft Holmes
                      wrote on last edited by
                      #11

                      As Danish said, this is a text sort, you need to check your data type.

                      Never underestimate the power of human stupidity RAH

                      1 Reply Last reply
                      0
                      • E eraser950

                        because without zeroes its not sorting in ascending like i have data 1 2 3 10 11 21 31 without zeroes its sorting like this 1 11 21 31 2 3

                        P Offline
                        P Offline
                        Pete OHanlon
                        wrote on last edited by
                        #12

                        eraser950 wrote:

                        because without zeroes its not sorting in ascending like i have data 1 2 3 10 11 21 31 without zeroes its sorting like this 1 11 21 31 2 3

                        That means they are stored as text, and not numbers. :rolleyes: Sorting numeric data types gives you the values in the right order.

                        I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                        Forgive your enemies - it messes with their heads

                        My blog | My articles | MoXAML PowerToys | Onyx

                        1 Reply Last reply
                        0
                        • A AndyInUK

                          Hi, I have a unique id column in a table with values - 12 32 45 345 3534 45456 etc. Now i want to make all the values to be 7 digit by placing 0's in front of each id for example above ids will be 0000012 0000032 0000045 0000345 0003534 0045456 Now there are 1000's of values that i need to change - what would be the best possible way to write query for this. Thanks

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #13

                          I assume mysql has similar sorts of functions but I have not had the pleasure, but in T-SQL / SQL Server you could do: select ID = right('0000000' + convert(varchar(20),ID),7) from MyTable -- ID can be int or varchar or select ID = right('0000000' + ID,7) from MyTable -- OK only when ID is varchar. Likewise you could put this into an update statement if for example you are in the process of converting your ID column from type int to varchar/text etc.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups