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. Which type should i use for saving a string like this: 000000-0000?

Which type should i use for saving a string like this: 000000-0000?

Scheduled Pinned Locked Moved Database
csharpdatabasesql-servervisual-studioalgorithms
12 Posts 4 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.
  • J Offline
    J Offline
    Jan Sommer
    wrote on last edited by
    #1

    Our customers are all id'ed by their social securitynumber (CPR) which is in the format 000000-0000 here in Denmark. How would you save this in the DB? The easiest way is ofcourse to save it as a string, but i'm also considering an integer-field. Does anyone know of the performance in searching when dealing with strings vs. integer fields (in a db), and would it be worth the effort to write code that converts between userinputted CPR's to integer? We are using MSSQL, and i'm programming in C# if it matters.

    B W 2 Replies Last reply
    0
    • J Jan Sommer

      Our customers are all id'ed by their social securitynumber (CPR) which is in the format 000000-0000 here in Denmark. How would you save this in the DB? The easiest way is ofcourse to save it as a string, but i'm also considering an integer-field. Does anyone know of the performance in searching when dealing with strings vs. integer fields (in a db), and would it be worth the effort to write code that converts between userinputted CPR's to integer? We are using MSSQL, and i'm programming in C# if it matters.

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      Why you don't use varchar column datatype in you DB? Why field in DB have to be integer datatype? I suggest to use varchar column in DB.


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      J 1 Reply Last reply
      0
      • B Blue_Boy

        Why you don't use varchar column datatype in you DB? Why field in DB have to be integer datatype? I suggest to use varchar column in DB.


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        J Offline
        J Offline
        Jan Sommer
        wrote on last edited by
        #3

        That is why i'm asking the question - i don't know. Won't the performance fall enough to take the cpr-fields datatype into consideration?

        B 1 Reply Last reply
        0
        • J Jan Sommer

          Our customers are all id'ed by their social securitynumber (CPR) which is in the format 000000-0000 here in Denmark. How would you save this in the DB? The easiest way is ofcourse to save it as a string, but i'm also considering an integer-field. Does anyone know of the performance in searching when dealing with strings vs. integer fields (in a db), and would it be worth the effort to write code that converts between userinputted CPR's to integer? We are using MSSQL, and i'm programming in C# if it matters.

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Store it as a string. We have a very similar format for SSN and it's proven to be best in string. If you want to search the whole SSN (using equality operator) or by describing the start of it (using LIKE '...%'), it will be efficient with string as long as you have an index on it. If you want to search efficiently using subparts of the SSN (for example Year BETWEEN XX and YY), you can break it down to other calculated fields such as year, month etc and index them. Create a trigger which fills the calculated fields and never let the user to modify them. Also if you can, enforce a uniqueness on the index on SSN, the search will be faster. Typically SSN should be unique but I don't know if you have exceptions and even if you do, do you encounter them in your application. Also use triggers to verify the correct format of SSN so you won't have accidental mistakes/duplicates. Hope this helps, Mika

          The need to optimize rises from a bad design.My articles[^]

          J 1 Reply Last reply
          0
          • J Jan Sommer

            That is why i'm asking the question - i don't know. Won't the performance fall enough to take the cpr-fields datatype into consideration?

            B Offline
            B Offline
            Blue_Boy
            wrote on last edited by
            #5

            As you know if you store value (example) 000012 as integer in DB then you will get only 12 value and zeros before 12 value will be eliminated automatically.So in your case I would use varchar datatype in DB.


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            R 1 Reply Last reply
            0
            • W Wendelius

              Store it as a string. We have a very similar format for SSN and it's proven to be best in string. If you want to search the whole SSN (using equality operator) or by describing the start of it (using LIKE '...%'), it will be efficient with string as long as you have an index on it. If you want to search efficiently using subparts of the SSN (for example Year BETWEEN XX and YY), you can break it down to other calculated fields such as year, month etc and index them. Create a trigger which fills the calculated fields and never let the user to modify them. Also if you can, enforce a uniqueness on the index on SSN, the search will be faster. Typically SSN should be unique but I don't know if you have exceptions and even if you do, do you encounter them in your application. Also use triggers to verify the correct format of SSN so you won't have accidental mistakes/duplicates. Hope this helps, Mika

              The need to optimize rises from a bad design.My articles[^]

              J Offline
              J Offline
              Jan Sommer
              wrote on last edited by
              #6

              Mika > Thanks a lot! That answer was very helpful. Blue_boy > You could format the integers when you show it to the user and easily insert the missing 0. But i'm convinced - i shall use varchar for SSN :)

              W B 2 Replies Last reply
              0
              • J Jan Sommer

                Mika > Thanks a lot! That answer was very helpful. Blue_boy > You could format the integers when you show it to the user and easily insert the missing 0. But i'm convinced - i shall use varchar for SSN :)

                W Offline
                W Offline
                Wendelius
                wrote on last edited by
                #7

                Jan Sommer wrote:

                Thanks a lot! That answer was very helpful

                No problem :)

                The need to optimize rises from a bad design.My articles[^]

                1 Reply Last reply
                0
                • J Jan Sommer

                  Mika > Thanks a lot! That answer was very helpful. Blue_boy > You could format the integers when you show it to the user and easily insert the missing 0. But i'm convinced - i shall use varchar for SSN :)

                  B Offline
                  B Offline
                  Blue_Boy
                  wrote on last edited by
                  #8

                  Jan Sommer wrote:

                  You could format the integers when you show it to the user and easily insert the missing 0

                  Yes,but your question was how to store value in DB like string or integer,by the way the SSN format 000000-0000 you can not store in DB as integer so you must use varchar.


                  I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                  R 1 Reply Last reply
                  0
                  • B Blue_Boy

                    As you know if you store value (example) 000012 as integer in DB then you will get only 12 value and zeros before 12 value will be eliminated automatically.So in your case I would use varchar datatype in DB.


                    I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                    R Offline
                    R Offline
                    Rob Graham
                    wrote on last edited by
                    #9

                    That's an easily corrected formatting issue, and not a reason to prefer integer over varchar.

                    B 1 Reply Last reply
                    0
                    • B Blue_Boy

                      Jan Sommer wrote:

                      You could format the integers when you show it to the user and easily insert the missing 0

                      Yes,but your question was how to store value in DB like string or integer,by the way the SSN format 000000-0000 you can not store in DB as integer so you must use varchar.


                      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                      R Offline
                      R Offline
                      Rob Graham
                      wrote on last edited by
                      #10

                      You have no idea what you are talking about. You are confusing data storage representation and UI visualization. They are not related.

                      B 1 Reply Last reply
                      0
                      • R Rob Graham

                        That's an easily corrected formatting issue, and not a reason to prefer integer over varchar.

                        B Offline
                        B Offline
                        Blue_Boy
                        wrote on last edited by
                        #11

                        :sigh:


                        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                        1 Reply Last reply
                        0
                        • R Rob Graham

                          You have no idea what you are talking about. You are confusing data storage representation and UI visualization. They are not related.

                          B Offline
                          B Offline
                          Blue_Boy
                          wrote on last edited by
                          #12

                          Rob Graham wrote:

                          You have no idea what you are talking about.

                          :wtf:


                          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

                          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