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 169 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.
  • 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