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. Other Discussions
  3. The Weird and The Wonderful
  4. Repurposing VarChar columns

Repurposing VarChar columns

Scheduled Pinned Locked Moved The Weird and The Wonderful
databasequestion
13 Posts 7 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.
  • D dbrenth

    So I brought our entire system to it's knees yesterday. What did I do? I entered some text into a varchar column in one of our database tables. Why was that bad? When this column was created it originally held text, but then somebody decided that this text would be better served in a lookup table. Instead of creating a new column for the lookup key (an integer), they simply changed each value in column to its corresponding number in the lookup table. And then they created views that join the table and lookup table which our company's programs tightly couple to. Microsoft, in their ever unceasing efforts to be helpful says you can join a varchar column to an integer column - so long as EVERY item in the varchar column casts to an integer. But if there is even one row that doesn't cast ... Why they couldn't cast the integer column to a varchar for us, only Microsoft can answer that. So, to make a long story short, I added my text as a new lookup item and now there is a number where my text used to be and everything and everybody is happy again.

    Brent

    P Offline
    P Offline
    PeejayAdams
    wrote on last edited by
    #4

    In fairness to Microsoft, they have introduced TRY_CAST() and TRY_CONVERT() in recent times which are very helpful when dealing with this kind thing.

    98.4% of statistics are made up on the spot.

    1 Reply Last reply
    0
    • Richard DeemingR Richard Deeming

      Jörgen Andersson wrote:

      If you make a join between an int and a varchar it wouldn't make a narrowing conversion on the varchar to int. It would make a widening conversion of the int to a varchar.

      Nope - int has a higher precedence than varchar. If you try to join int to varchar, SQL will try to convert all values to int. Data Type Precedence (Transact-SQL) | Microsoft Docs[^]


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      J Offline
      J Offline
      Jorgen Andersson
      wrote on last edited by
      #5

      Fuck me, you're right. But why?

      Wrong is evil and must be defeated. - Jeff Ello

      L 1 Reply Last reply
      0
      • D dbrenth

        So I brought our entire system to it's knees yesterday. What did I do? I entered some text into a varchar column in one of our database tables. Why was that bad? When this column was created it originally held text, but then somebody decided that this text would be better served in a lookup table. Instead of creating a new column for the lookup key (an integer), they simply changed each value in column to its corresponding number in the lookup table. And then they created views that join the table and lookup table which our company's programs tightly couple to. Microsoft, in their ever unceasing efforts to be helpful says you can join a varchar column to an integer column - so long as EVERY item in the varchar column casts to an integer. But if there is even one row that doesn't cast ... Why they couldn't cast the integer column to a varchar for us, only Microsoft can answer that. So, to make a long story short, I added my text as a new lookup item and now there is a number where my text used to be and everything and everybody is happy again.

        Brent

        G Offline
        G Offline
        GuyThiebaut
        wrote on last edited by
        #6

        How about creating a foreign key relationship between the tables? Would that not prevent this issue from happening again?

        “That which can be asserted without evidence, can be dismissed without evidence.”

        ― Christopher Hitchens

        D 1 Reply Last reply
        0
        • J Jorgen Andersson

          Fuck me, you're right. But why?

          Wrong is evil and must be defeated. - Jeff Ello

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

          Comparing ints is faster, and probably seen as the default.

          Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

          J 1 Reply Last reply
          0
          • L Lost User

            Comparing ints is faster, and probably seen as the default.

            Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

            J Offline
            J Offline
            Jorgen Andersson
            wrote on last edited by
            #8

            Yes, but a widening conversion to varchar and comparison between two varchars are probably faster than a narrowing conversion to int with an error check and then comparing two ints.

            Wrong is evil and must be defeated. - Jeff Ello

            L 1 Reply Last reply
            0
            • J Jorgen Andersson

              Yes, but a widening conversion to varchar and comparison between two varchars are probably faster than a narrowing conversion to int with an error check and then comparing two ints.

              Wrong is evil and must be defeated. - Jeff Ello

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

              Jörgen Andersson wrote:

              with an error check and then comparing two ints.

              It's probably more a cast than a conversion.

              Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

              J 1 Reply Last reply
              0
              • L Lost User

                Jörgen Andersson wrote:

                with an error check and then comparing two ints.

                It's probably more a cast than a conversion.

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] "If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

                J Offline
                J Offline
                Jorgen Andersson
                wrote on last edited by
                #10

                Probably.

                Wrong is evil and must be defeated. - Jeff Ello

                1 Reply Last reply
                0
                • G GuyThiebaut

                  How about creating a foreign key relationship between the tables? Would that not prevent this issue from happening again?

                  “That which can be asserted without evidence, can be dismissed without evidence.”

                  ― Christopher Hitchens

                  D Offline
                  D Offline
                  David A Gray
                  wrote on last edited by
                  #11

                  GuyThiebaut wrote:

                  How about creating a foreign key relationship between the tables?

                  I believe that would trade one problem for another; you would trade an invalid cast for a foreign key constraint exception, which might well roll back a whole transaction. Moreover, who knows what it would do to the view?

                  David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                  G 1 Reply Last reply
                  0
                  • D David A Gray

                    GuyThiebaut wrote:

                    How about creating a foreign key relationship between the tables?

                    I believe that would trade one problem for another; you would trade an invalid cast for a foreign key constraint exception, which might well roll back a whole transaction. Moreover, who knows what it would do to the view?

                    David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                    G Offline
                    G Offline
                    GuyThiebaut
                    wrote on last edited by
                    #12

                    Exactly, which is why foreign keys can be useful as they will prevent the insertion of data into a database which does not meet certain criteria. A foreign key constraint should not affect a view - I see it more as a gatekeeper that prevents problematic data from being added to a database, whether that is by insertion, deletion or update. I am aware that many people do not use foreign keys, however I have seen them to help in making the developer's life easier by keeping problematic data out of a database. Without foreign keys you can find yourself in the position where the client is complaining about the system behaving strangely when the reason for that behaviour is, for want of a better word, unsanitised data.

                    “That which can be asserted without evidence, can be dismissed without evidence.”

                    ― Christopher Hitchens

                    D 1 Reply Last reply
                    0
                    • G GuyThiebaut

                      Exactly, which is why foreign keys can be useful as they will prevent the insertion of data into a database which does not meet certain criteria. A foreign key constraint should not affect a view - I see it more as a gatekeeper that prevents problematic data from being added to a database, whether that is by insertion, deletion or update. I am aware that many people do not use foreign keys, however I have seen them to help in making the developer's life easier by keeping problematic data out of a database. Without foreign keys you can find yourself in the position where the client is complaining about the system behaving strangely when the reason for that behaviour is, for want of a better word, unsanitised data.

                      “That which can be asserted without evidence, can be dismissed without evidence.”

                      ― Christopher Hitchens

                      D Offline
                      D Offline
                      David A Gray
                      wrote on last edited by
                      #13

                      GuyThiebaut wrote:

                      have seen them to help in making the developer's life easier by keeping problematic data out of a database.

                      I agree with you 100%, and I rely on foreign keys for precisely that reason. Nevertheless, I think it's important to be aware that having one doesn't excuse the developer from handling exceptions. In the situation under discussion, you are trading one kind of exception for another, albeit, more useful, one. I don't think it would affect the view, either, but I thought it prudent to allow for some technical detail that I might have overlooked. I usually don't construct any views until I have my foreign keys defined, so that I can leverage them to help create the views.

                      David A. Gray Delivering Solutions for the Ages, One Problem at a Time Interpreting the Fundamental Principle of Tabular Reporting

                      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