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 115 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.
  • 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;

    B Offline
    B Offline
    Bruce Patin
    wrote on last edited by
    #31

    You can't do that without IBM's new Infinite Storage. But they're in short supply. Their first customer ordered two.

    G 1 Reply Last reply
    0
    • B Bruce Patin

      You can't do that without IBM's new Infinite Storage. But they're in short supply. Their first customer ordered two.

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

      2, he said. Snicker, chortle, guffaw. :-D

      Software Zen: delete this;

      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

        E Offline
        E Offline
        englebart
        wrote on last edited by
        #33

        In a DB that blank pads all columns to their fixed size, this really makes sense. [LEN (Transact-SQL) | Microsoft Docs](https://docs.microsoft.com/en-us/sql/t-sql/functions/len-transact-sql) snip Remarks LEN excludes trailing blanks. If that is a problem, consider using the DATALENGTH (Transact-SQL) function which does not trim the string. If processing a unicode string, DATALENGTH will return twice the number of characters. The following example demonstrates LEN and DATALENGTH with a trailing space.

        C 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

          B Offline
          B Offline
          Bassam Abdul Baki
          wrote on last edited by
          #34

          I did. I missed your "hands up" part though the first time around.

          Web - BM - RSS - Math - LinkedIn

          1 Reply Last reply
          0
          • E englebart

            In a DB that blank pads all columns to their fixed size, this really makes sense. [LEN (Transact-SQL) | Microsoft Docs](https://docs.microsoft.com/en-us/sql/t-sql/functions/len-transact-sql) snip Remarks LEN excludes trailing blanks. If that is a problem, consider using the DATALENGTH (Transact-SQL) function which does not trim the string. If processing a unicode string, DATALENGTH will return twice the number of characters. The following example demonstrates LEN and DATALENGTH with a trailing space.

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

            Yes - I understand the function and how it works. My point was that I'd missed that tiny little proviso. For years. It shocked me. Storing padding data inside a database? Please no. That's awful.

            cheers Chris Maunder

            E 1 Reply Last reply
            0
            • L Luiz_LFM

              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 Offline
              C Offline
              Chris Maunder
              wrote on last edited by
              #36

              Exactly!

              cheers Chris Maunder

              1 Reply 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;

                R Offline
                R Offline
                rnbergren
                wrote on last edited by
                #37

                don't even get me started on that. I have been burned so many times on the zero length spaces. They are very spacey

                To err is human to really mess up you need a computer

                G 1 Reply Last reply
                0
                • R rnbergren

                  don't even get me started on that. I have been burned so many times on the zero length spaces. They are very spacey

                  To err is human to really mess up you need a computer

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

                  rnbergren wrote:

                  I have been burned so many times on the zero length spaces

                  I actually found a good use for them. I had a comma-separated value file I was dealing with, and I didn't want embedded line breaks. I replaced the line breaks with zero-width spaces, and voila! As I recall, even Excel handled them correctly.

                  Software Zen: delete this;

                  1 Reply Last reply
                  0
                  • J Jan Holst Jensen2

                    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 Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #39

                    Jan Holst Jensen2 wrote:

                    Yup. I know this is a question about SQL Server, but I have a feeling that they have adopted some old logic from Oracle.

                    Could be. The timeline makes it possible. Sybase (precursor to SQL Server) was founded after Oracle.

                    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

                      M Offline
                      M Offline
                      Member_5893260
                      wrote on last edited by
                      #40

                      It's a fudge, implemented because of the 'char' fixed-length datatype:

                      declare @a char(10)
                      set @a=''
                      print len(@a) -- =0. But it doesn't. In reality, @a is ten spaces...
                      set @a='a'
                      print @a+'b' -- prints "a b"
                      print len(@a) -- =1

                      So there it is... I hate it too.

                      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
                        #41

                        Alas, I just got smacked by this today even though I know not to trust LEN. In this case, though I was using VARBINARY(16) to hold binary IP addresses -- and testing the length to determine IPv4 vs IPv6. And today someone reported that it fails for IP addresses where the last byte is 32. :sigh:

                        1 Reply Last reply
                        0
                        • C Chris Maunder

                          Yes - I understand the function and how it works. My point was that I'd missed that tiny little proviso. For years. It shocked me. Storing padding data inside a database? Please no. That's awful.

                          cheers Chris Maunder

                          E Offline
                          E Offline
                          englebart
                          wrote on last edited by
                          #42

                          For a short field size, it is more efficient to just pad it out. Say a 4 byte field that is only storing 3 bytes of a string.

                          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