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. generate specific format in sql table

generate specific format in sql table

Scheduled Pinned Locked Moved Database
database
8 Posts 5 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.
  • S Offline
    S Offline
    sk_ko
    wrote on last edited by
    #1

    I want to add one column in sql table. That will be in specific format. The format is like that Year+00001 Year+00002 ....and so on. If the year changes to new year, I want to start from 00001. like that. Year+00001 Year+00002 to be more clear what I need is: like below in applicationID column. name | date | applicationID Willam | 2011-12-03 | 201100001 Susan | 2011-12-04 | 201100002 Alex | 2012-01-01 | 201200001 Mata | 2013-01-01 | 201300001 how I have to do. & please.

    M P J L 4 Replies Last reply
    0
    • S sk_ko

      I want to add one column in sql table. That will be in specific format. The format is like that Year+00001 Year+00002 ....and so on. If the year changes to new year, I want to start from 00001. like that. Year+00001 Year+00002 to be more clear what I need is: like below in applicationID column. name | date | applicationID Willam | 2011-12-03 | 201100001 Susan | 2011-12-04 | 201100002 Alex | 2012-01-01 | 201200001 Mata | 2013-01-01 | 201300001 how I have to do. & please.

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

      I suspect this is a BAD idea, I also suspect you are using this field as a primary key. If so the DON'T it is a BAD design. To do this you will need to query the table to find out how many inserts there are for this year, build your string and insert the record. There are a number of alternatives to this process that include triggers and stored procedures. Here is the reason it is a bad idea. What happens if another user inserts a record during this process, BOOM one busted database. I suggest you insert the datetime for Created then query filtered on the year and ordered by the created date. If you need an incremental number you can use ROW_NUMBER() assuming you are using sql server

      Never underestimate the power of human stupidity RAH

      S 1 Reply Last reply
      0
      • M Mycroft Holmes

        I suspect this is a BAD idea, I also suspect you are using this field as a primary key. If so the DON'T it is a BAD design. To do this you will need to query the table to find out how many inserts there are for this year, build your string and insert the record. There are a number of alternatives to this process that include triggers and stored procedures. Here is the reason it is a bad idea. What happens if another user inserts a record during this process, BOOM one busted database. I suggest you insert the datetime for Created then query filtered on the year and ordered by the created date. If you need an incremental number you can use ROW_NUMBER() assuming you are using sql server

        Never underestimate the power of human stupidity RAH

        S Offline
        S Offline
        sk_ko
        wrote on last edited by
        #3

        hi, I am not clear your answer... ok, how about adding auto generated id(identity) column in my table.. I will use id column as primary key. I will not use applicationID column as primary key. for applicationID column, I just want to store in this format. id | name | date | applicationID 1 | Willam | 2011-12-03 | 201100001 2 | Susan | 2011-12-04 | 201100002 3 | Alex | 2012-01-01 | 201200001 4 | Mata | 2013-01-01 | 201300001 pls.

        M 1 Reply Last reply
        0
        • S sk_ko

          hi, I am not clear your answer... ok, how about adding auto generated id(identity) column in my table.. I will use id column as primary key. I will not use applicationID column as primary key. for applicationID column, I just want to store in this format. id | name | date | applicationID 1 | Willam | 2011-12-03 | 201100001 2 | Susan | 2011-12-04 | 201100002 3 | Alex | 2012-01-01 | 201200001 4 | Mata | 2013-01-01 | 201300001 pls.

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

          Better design! Then you can update the record after it is inserted to calculated the applicationid field. You will need to pad the ROW_NUMBER()/MAX value with leading zeros and combine that with the year datepart. This is a better option as the ApplicationID field that is a human consumed piece of info is not used to identify the record.

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          • S sk_ko

            I want to add one column in sql table. That will be in specific format. The format is like that Year+00001 Year+00002 ....and so on. If the year changes to new year, I want to start from 00001. like that. Year+00001 Year+00002 to be more clear what I need is: like below in applicationID column. name | date | applicationID Willam | 2011-12-03 | 201100001 Susan | 2011-12-04 | 201100002 Alex | 2012-01-01 | 201200001 Mata | 2013-01-01 | 201300001 how I have to do. & please.

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

            Here's a similar thread from a few months back: http://www.codeproject.com/Messages/4031000/Re-generating-IDs-with-IDENTITY-in-sql-server-2005.aspx[^]

            S 1 Reply Last reply
            0
            • P PIEBALDconsult

              Here's a similar thread from a few months back: http://www.codeproject.com/Messages/4031000/Re-generating-IDs-with-IDENTITY-in-sql-server-2005.aspx[^]

              S Offline
              S Offline
              sk_ko
              wrote on last edited by
              #6

              I saw same problem like me. but I didn't see best answer, like sample.

              1 Reply Last reply
              0
              • S sk_ko

                I want to add one column in sql table. That will be in specific format. The format is like that Year+00001 Year+00002 ....and so on. If the year changes to new year, I want to start from 00001. like that. Year+00001 Year+00002 to be more clear what I need is: like below in applicationID column. name | date | applicationID Willam | 2011-12-03 | 201100001 Susan | 2011-12-04 | 201100002 Alex | 2012-01-01 | 201200001 Mata | 2013-01-01 | 201300001 how I have to do. & please.

                J Offline
                J Offline
                jschell
                wrote on last edited by
                #7

                There is also a security reason for not doing this. If someone sees that their id is "20110005" it is easy to guess that 20110004 and 20110006 are also valid.

                1 Reply Last reply
                0
                • S sk_ko

                  I want to add one column in sql table. That will be in specific format. The format is like that Year+00001 Year+00002 ....and so on. If the year changes to new year, I want to start from 00001. like that. Year+00001 Year+00002 to be more clear what I need is: like below in applicationID column. name | date | applicationID Willam | 2011-12-03 | 201100001 Susan | 2011-12-04 | 201100002 Alex | 2012-01-01 | 201200001 Mata | 2013-01-01 | 201300001 how I have to do. & please.

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

                  sankooo wrote:

                  I want to add one column in sql table.
                  That will be in specific format.

                  Mycroft is right, but it's half the story. Tables hold data, not information.

                  sankooo wrote:

                  If the year changes to new year, I want to start from 00001. like that.

                  That's a composite column, and in the ideal world we try to keep the data atomic. That means that you can loose the date-part, as that's already encoded in the previous column. You also don't need the prefix-zeroes, those are merely there for presentation. Hence, all you need in the ApplicationID-row is a sequence-number; all other information is already present and the complete applicationID as you need can be calculated from the rest of the tuple. The other half of the story is that we don't want to duplicate information, as that would be redundant (and thus, increases the chances for errors). I'd suggest you read up on normalization[^] procedures.

                  Bastard Programmer from Hell :suss:

                  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