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. Help me understand this unusual query... [modified]

Help me understand this unusual query... [modified]

Scheduled Pinned Locked Moved Database
databasehelptoolsquestion
11 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.
  • R Offline
    R Offline
    Rafferty Uy
    wrote on last edited by
    #1

    Hi, There was a small problem with our app where it allowed users to enter white-space only characters in one of the data fields. So now I'm trying to clean this up. A colleague of mine sent me this unusual SQL query to find the whitespaces. The script looks like this:

    SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
    FROM Table1
    WHERE ASCII(LEFT(tblField1,1)) = 32

    This query returned 40 rows. Now, when I try either of the following:

    SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
    FROM Table1
    WHERE tblField1 = ' ' -- with a single whitespace

    or

    SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
    FROM Table1
    WHERE tblField1 = '' -- empty string

    The above 2 queries return 200,000+ rows. Why is this so?

    Rafferty

    modified on Sunday, December 27, 2009 10:23 PM

    L M 2 Replies Last reply
    0
    • R Rafferty Uy

      Hi, There was a small problem with our app where it allowed users to enter white-space only characters in one of the data fields. So now I'm trying to clean this up. A colleague of mine sent me this unusual SQL query to find the whitespaces. The script looks like this:

      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
      FROM Table1
      WHERE ASCII(LEFT(tblField1,1)) = 32

      This query returned 40 rows. Now, when I try either of the following:

      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
      FROM Table1
      WHERE tblField1 = ' ' -- with a single whitespace

      or

      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
      FROM Table1
      WHERE tblField1 = '' -- empty string

      The above 2 queries return 200,000+ rows. Why is this so?

      Rafferty

      modified on Sunday, December 27, 2009 10:23 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, the first query selects all rows where tblField1 holds a string with a first character equal to a space. the other two require the entire tblField1 to equal something that probably gets trimmed down to an empty string, hence returning all rows with that field empty. That obviously is quite a different query. IMO the more elegant query would use

      ...WHERE tblField1 LIKE ' %'

      using a single space and the wildcard character %. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Merry Christmas and a Happy New Year to all.


      R 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, the first query selects all rows where tblField1 holds a string with a first character equal to a space. the other two require the entire tblField1 to equal something that probably gets trimmed down to an empty string, hence returning all rows with that field empty. That obviously is quite a different query. IMO the more elegant query would use

        ...WHERE tblField1 LIKE ' %'

        using a single space and the wildcard character %. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Merry Christmas and a Happy New Year to all.


        R Offline
        R Offline
        Rafferty Uy
        wrote on last edited by
        #3

        So the = operator does not check for exact string equality... I knew before that = is case insensitive but to think that it also trims the string! Is LIKE the more preferable conditional operator then? Is there a performance difference between = and LIKE? Say for example WHERE tblField1 = 'something' vs. WHERE tblField1 LIKE 'something'

        Rafferty

        L 1 Reply Last reply
        0
        • R Rafferty Uy

          So the = operator does not check for exact string equality... I knew before that = is case insensitive but to think that it also trims the string! Is LIKE the more preferable conditional operator then? Is there a performance difference between = and LIKE? Say for example WHERE tblField1 = 'something' vs. WHERE tblField1 LIKE 'something'

          Rafferty

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          1. I'm not sure, I think it stripped the spaces, got an empty constant, and returned all records. 2. LIKE makes most sense when it contains a wildcard, and = does not accept wildcards; without wildcards databases can take big advantage of indexing, with wildcards they can't, so most often LIKE is much slower. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Merry Christmas and a Happy New Year to all.


          R 1 Reply Last reply
          0
          • L Luc Pattyn

            1. I'm not sure, I think it stripped the spaces, got an empty constant, and returned all records. 2. LIKE makes most sense when it contains a wildcard, and = does not accept wildcards; without wildcards databases can take big advantage of indexing, with wildcards they can't, so most often LIKE is much slower. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Merry Christmas and a Happy New Year to all.


            R Offline
            R Offline
            Rafferty Uy
            wrote on last edited by
            #5

            So using LIKE will not use the database indexing even if there are no wildcards in the passed string?

            Rafferty

            L W 2 Replies Last reply
            0
            • R Rafferty Uy

              So using LIKE will not use the database indexing even if there are no wildcards in the passed string?

              Rafferty

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              I don't know what goes on inside a specific database, that is up to the guys implementing all of it; it may or may not be documented, and it might depend on circumstances, such as available memory and disk space. The simple rule is: use LIKE if you need a wildcard, and expect it to be slower. If you need more, look for the documentation and perform your own experiments (on sufficiently large data sets). :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              Merry Christmas and a Happy New Year to all.


              R 1 Reply Last reply
              0
              • L Luc Pattyn

                I don't know what goes on inside a specific database, that is up to the guys implementing all of it; it may or may not be documented, and it might depend on circumstances, such as available memory and disk space. The simple rule is: use LIKE if you need a wildcard, and expect it to be slower. If you need more, look for the documentation and perform your own experiments (on sufficiently large data sets). :)

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                Merry Christmas and a Happy New Year to all.


                R Offline
                R Offline
                Rafferty Uy
                wrote on last edited by
                #7

                hm... maybe I'll do some experimenting when I find the time. Thanks for your help!

                Rafferty

                L 1 Reply Last reply
                0
                • R Rafferty Uy

                  hm... maybe I'll do some experimenting when I find the time. Thanks for your help!

                  Rafferty

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  You're welcome. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  Merry Christmas and a Happy New Year to all.


                  1 Reply Last reply
                  0
                  • R Rafferty Uy

                    So using LIKE will not use the database indexing even if there are no wildcards in the passed string?

                    Rafferty

                    W Offline
                    W Offline
                    WoutL
                    wrote on last edited by
                    #9

                    Microsoft SQLServer will use indexes (if available) when you use the like statement without wildcards. Even if you do use wildcards, SQL will use indexes if possible. if you compare to a string witch starts with a wildcard, SQL will NOT use indexes.

                    Wout Louwers

                    R 1 Reply Last reply
                    0
                    • R Rafferty Uy

                      Hi, There was a small problem with our app where it allowed users to enter white-space only characters in one of the data fields. So now I'm trying to clean this up. A colleague of mine sent me this unusual SQL query to find the whitespaces. The script looks like this:

                      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
                      FROM Table1
                      WHERE ASCII(LEFT(tblField1,1)) = 32

                      This query returned 40 rows. Now, when I try either of the following:

                      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
                      FROM Table1
                      WHERE tblField1 = ' ' -- with a single whitespace

                      or

                      SELECT tblId, ASCII(tblField1) AS 'ASCII Code', tblField1
                      FROM Table1
                      WHERE tblField1 = '' -- empty string

                      The above 2 queries return 200,000+ rows. Why is this so?

                      Rafferty

                      modified on Sunday, December 27, 2009 10:23 PM

                      M Offline
                      M Offline
                      Member 4501940
                      wrote on last edited by
                      #10

                      Something is wrong...in ansi sql databases: The rows returned in the first query will be equal to or greater than the second. All of the rows in the second will be included in the first. The rows returned in the third query will never be in the first or second. Need to figure this out before thinking about indexes and wildcards. I would change the second qry as such: WHERE tblField1 = char(32) just to be sure because-

                      select ascii(' ') --this equals 9
                      select ascii(' ') --this equals 32

                      Create and put data in your Table1 and test if you are not convinced.

                      1 Reply Last reply
                      0
                      • W WoutL

                        Microsoft SQLServer will use indexes (if available) when you use the like statement without wildcards. Even if you do use wildcards, SQL will use indexes if possible. if you compare to a string witch starts with a wildcard, SQL will NOT use indexes.

                        Wout Louwers

                        R Offline
                        R Offline
                        Rafferty Uy
                        wrote on last edited by
                        #11

                        Wow this is very useful! Thanks a lot! :thumbsup:

                        Rafferty

                        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