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. The Lounge
  3. SQL does my head in

SQL does my head in

Scheduled Pinned Locked Moved The Lounge
databasequestion
42 Posts 24 Posters 129 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 jschell

    Chris Maunder wrote:

    Hands up who has ever noticed the "excluding trailing blanks" bit?

    Me. It amazed me the first time I discovered it as well. I only figured it out after trying to figure out why some code was failing to behave as it should. I added documentation to the stored procedure to clearly call it out, so the next developer (perhaps even me) would not be surprised either. I believe, but I could be mistaken, it only applies to a the data type 'char'. Your constant is a 'char'. It behaves as expected with 'varchar'.

    S Offline
    S Offline
    Slacker007
    wrote on last edited by
    #8

    I wonder if this has anything to do with CHAR is fixed length and VARCHAR is variable length?

    J 1 Reply Last reply
    0
    • C Chris Maunder

      LEN (Transact-SQL) Returns the number of characters of the specified string expression, excluding trailing blanks So

      > print len('hello')

      5

      and

      > print len(' ')

      0

      Hands up who has ever noticed the "excluding trailing blanks" bit? Up until an hour ago, not me. Which boggles my mind.

      cheers Chris Maunder

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #9

      If this was QA, I'd be fighting the urge - a strong urge - to reply "RTFM". But it isn't, so I don't have to... :laugh:

      Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      S C 2 Replies Last reply
      0
      • OriginalGriffO OriginalGriff

        If this was QA, I'd be fighting the urge - a strong urge - to reply "RTFM". But it isn't, so I don't have to... :laugh:

        Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        S Offline
        S Offline
        Slacker007
        wrote on last edited by
        #10

        but you already did. :laugh: so now you don't have to. Even more :laugh: :laugh:

        1 Reply Last reply
        0
        • C Chris Maunder

          LEN (Transact-SQL) Returns the number of characters of the specified string expression, excluding trailing blanks So

          > print len('hello')

          5

          and

          > print len(' ')

          0

          Hands up who has ever noticed the "excluding trailing blanks" bit? Up until an hour ago, not me. Which boggles my mind.

          cheers Chris Maunder

          S Offline
          S Offline
          Slacker007
          wrote on last edited by
          #11

          SELECT DATALENGTH(' ');

          > 5

          1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            If this was QA, I'd be fighting the urge - a strong urge - to reply "RTFM". But it isn't, so I don't have to... :laugh:

            Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

            C Offline
            C Offline
            Chris Maunder
            wrote on last edited by
            #12

            This is the point: I've read the manual a million times. And I never noticed the proviso. What kills me, I think (apart from noisy bagpipes and people who don't indicate when turning) is that they've taken a method (LEN) that is used everywhere for the same thing - get the length of something - and bent it slightly. The length of something except for a bit at the end, sometimes. I propose that we, immediately, rename DATALENGTH to LEN, and LEN to LENWITHOUTTHETRAILINGSPACES, and to hell with the destruction this will cause to civilisation.

            cheers Chris Maunder

            N L 2 Replies Last reply
            0
            • C Chris Maunder

              Exactly!

              cheers Chris Maunder

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #13

              So it should return 1? :laugh:

              Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              1 Reply Last reply
              0
              • V Vark111

                print len(' ')

                Those look like leading blanks to me, not trailing blanks.

                C Offline
                C Offline
                CodeWraith
                wrote on last edited by
                #14

                And you will only make that mistake twice: For the first and the last time. :-)

                I am endeavoring, ma'am, to construct a mnemonic memory circuit using stone knives and bearskins.

                N 1 Reply Last reply
                0
                • C Chris Maunder

                  LEN (Transact-SQL) Returns the number of characters of the specified string expression, excluding trailing blanks So

                  > print len('hello')

                  5

                  and

                  > print len(' ')

                  0

                  Hands up who has ever noticed the "excluding trailing blanks" bit? Up until an hour ago, not me. Which boggles my mind.

                  cheers Chris Maunder

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

                  Sooo... there are probably other things for which you have not yet RTFMed?

                  C 1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Sooo... there are probably other things for which you have not yet RTFMed?

                    C Offline
                    C Offline
                    Chris Maunder
                    wrote on last edited by
                    #16

                    Does this scare you? ;)

                    cheers Chris Maunder

                    1 Reply Last reply
                    0
                    • V Vark111

                      print len(' ')

                      Those look like leading blanks to me, not trailing blanks.

                      N Offline
                      N Offline
                      Nish Nishant
                      wrote on last edited by
                      #17

                      I see 2 trailing blanks and 2 leading blanks surrounding a single blank.

                      Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                      R G 2 Replies Last reply
                      0
                      • N Nish Nishant

                        I see 2 trailing blanks and 2 leading blanks surrounding a single blank.

                        Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                        R Offline
                        R Offline
                        Rajesh R Subramanian
                        wrote on last edited by
                        #18

                        This discussion is turning rather spacey.

                        L 1 Reply Last reply
                        0
                        • R Rajesh R Subramanian

                          This discussion is turning rather spacey.

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

                          Now you are shooting blanks

                          1 Reply Last reply
                          0
                          • C Chris Maunder

                            This is the point: I've read the manual a million times. And I never noticed the proviso. What kills me, I think (apart from noisy bagpipes and people who don't indicate when turning) is that they've taken a method (LEN) that is used everywhere for the same thing - get the length of something - and bent it slightly. The length of something except for a bit at the end, sometimes. I propose that we, immediately, rename DATALENGTH to LEN, and LEN to LENWITHOUTTHETRAILINGSPACES, and to hell with the destruction this will cause to civilisation.

                            cheers Chris Maunder

                            N Offline
                            N Offline
                            Nathan Minier
                            wrote on last edited by
                            #20

                            Or, I dunno, embrace object databases as a profession. Just saying.

                            "There are three kinds of lies: lies, damned lies and statistics." - Benjamin Disraeli

                            1 Reply Last reply
                            0
                            • C CodeWraith

                              And you will only make that mistake twice: For the first and the last time. :-)

                              I am endeavoring, ma'am, to construct a mnemonic memory circuit using stone knives and bearskins.

                              N Offline
                              N Offline
                              Nelek
                              wrote on last edited by
                              #21

                              Human being is the only animal that stumble twice with the same stone

                              M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you Rating helpful answers is nice, but saying thanks can be even nicer.

                              1 Reply Last reply
                              0
                              • N Nish Nishant

                                I see 2 trailing blanks and 2 leading blanks surrounding a single blank.

                                Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                G Offline
                                G Offline
                                Gary Wheeler
                                wrote on last edited by
                                #22

                                Hmm. Actually, there could be an infinite number of zero width spaces[^] in that string in addition to the 'normal' spaces, if you're using UNICODE.

                                Software Zen: delete this;

                                N B R 3 Replies Last reply
                                0
                                • G Gary Wheeler

                                  Hmm. Actually, there could be an infinite number of zero width spaces[^] in that string in addition to the 'normal' spaces, if you're using UNICODE.

                                  Software Zen: delete this;

                                  N Offline
                                  N Offline
                                  Nish Nishant
                                  wrote on last edited by
                                  #23

                                  Dear lord! :eek:

                                  Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                  G 1 Reply Last reply
                                  0
                                  • N Nish Nishant

                                    Dear lord! :eek:

                                    Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                    G Offline
                                    G Offline
                                    Gary Wheeler
                                    wrote on last edited by
                                    #24

                                    One of the hazards of being the UI guy, and all of your UI's are localized - you can make UNICODE jokes :sigh:.

                                    Software Zen: delete this;

                                    N 1 Reply Last reply
                                    0
                                    • G Gary Wheeler

                                      One of the hazards of being the UI guy, and all of your UI's are localized - you can make UNICODE jokes :sigh:.

                                      Software Zen: delete this;

                                      N Offline
                                      N Offline
                                      Nish Nishant
                                      wrote on last edited by
                                      #25

                                      :laugh:

                                      Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com

                                      1 Reply Last reply
                                      0
                                      • C Chris Maunder

                                        This is the point: I've read the manual a million times. And I never noticed the proviso. What kills me, I think (apart from noisy bagpipes and people who don't indicate when turning) is that they've taken a method (LEN) that is used everywhere for the same thing - get the length of something - and bent it slightly. The length of something except for a bit at the end, sometimes. I propose that we, immediately, rename DATALENGTH to LEN, and LEN to LENWITHOUTTHETRAILINGSPACES, and to hell with the destruction this will cause to civilisation.

                                        cheers Chris Maunder

                                        L Offline
                                        L Offline
                                        Luiz_LFM
                                        wrote on last edited by
                                        #26

                                        Holy crap, I propose T-SQL gets killed and replaced with ANSI SQL... I've been working with enterprise databases for almost 15 years now (plus a few more in general programming) and never have I ever read a manual for a string length function! You just google " string lenght" and notice the snippet of the first post "select len(mycol) from.." and there you go, you know it. I would NEVER expect a len() to function as len(rtrim())... that's just insane; any other programming languages do this sort of (sorry, but it is...) idiotic thing? :wtf: The more I learn about TSQL, the more I enjoy the fact the most of the bigger companies use Oracle DB... :-O Maybe next time I get a client with MS (so rare, w00t), I'm really gonna have to RT(whole)FM, even for very standard functions like len()!!?!? Insane, I tell you! Maybe I'll find that substr() doesn't consider ponctuation or something...??

                                        C 1 Reply Last reply
                                        0
                                        • S Slacker007

                                          I wonder if this has anything to do with CHAR is fixed length and VARCHAR is variable length?

                                          J Offline
                                          J Offline
                                          Jan Holst Jensen2
                                          wrote on last edited by
                                          #27

                                          Slacker007 wrote:

                                          I wonder if this has anything to do with CHAR is fixed length and VARCHAR is variable length?

                                          Yup. I know this is a question about SQL Server, but I have a feeling that they have adopted some old logic from Oracle. In Oracle the CHAR type will automatically pad data with spaces so the data field always occupies the max length you specified. CHAR versus VARCHAR2 Semantics[^] In that context, the behavior of len() is actually the only one that makes sense :wtf: . So, to keep your sanity you never want to use CHAR but always VARCHAR/VARCHAR2 :-). Or, in Postgres - just use TEXT to get rid of all those pesky length constraints as well.

                                          J 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