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. Clever Code
  4. SQL "bug"

SQL "bug"

Scheduled Pinned Locked Moved Clever Code
databasehelp
10 Posts 5 Posters 2 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.
  • H Offline
    H Offline
    Howard Richards
    wrote on last edited by
    #1

    I was having a hard time figuring out why my T-SQL query wasn't working.

    DECLARE @billid int, @cli varchar;
    SET @billid =12345;
    SET @cli = '02077778888';

    SELECT COUNT(*) FROM Records WHERE BillID = @billid AND CLI = @cli;

    This query consistently returned a result of zero records, despite my knowing that records existed for this combination, e.g.:

    SELECT COUNT(*) FROM Records WHERE BillID = 12345
    AND CLI = '02077778888';

    would return the correct number. Solution: The length of the varchar had not been specified. Although it parses correctly it doesn't actually work without a length being specified. This simple change fixed things:

    DECLARE @billid int, @cli varchar(14);

    'Howard

    R I 2 Replies Last reply
    0
    • H Howard Richards

      I was having a hard time figuring out why my T-SQL query wasn't working.

      DECLARE @billid int, @cli varchar;
      SET @billid =12345;
      SET @cli = '02077778888';

      SELECT COUNT(*) FROM Records WHERE BillID = @billid AND CLI = @cli;

      This query consistently returned a result of zero records, despite my knowing that records existed for this combination, e.g.:

      SELECT COUNT(*) FROM Records WHERE BillID = 12345
      AND CLI = '02077778888';

      would return the correct number. Solution: The length of the varchar had not been specified. Although it parses correctly it doesn't actually work without a length being specified. This simple change fixed things:

      DECLARE @billid int, @cli varchar(14);

      'Howard

      R Offline
      R Offline
      Robert Royall
      wrote on last edited by
      #2

      I've had something similar happen with some user authentication work I was doing. I was generating an MD5 password hash and sending it to SQL Server 2000 for verification but even though I checked it for two hours, the application would never verify the password correctly even though it worked when I wrote out the SQL statement in Enterprise Manager. It turned out to be essentially a cast error - string literals in SQL statements are considered by SQL2k to be varchars, while my application was passing in an nvarchar field, and the blank Unicode bytes were throwing off the hash comparison.

      Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world. -Me explaining my job to an engineer

      H B 2 Replies Last reply
      0
      • R Robert Royall

        I've had something similar happen with some user authentication work I was doing. I was generating an MD5 password hash and sending it to SQL Server 2000 for verification but even though I checked it for two hours, the application would never verify the password correctly even though it worked when I wrote out the SQL statement in Enterprise Manager. It turned out to be essentially a cast error - string literals in SQL statements are considered by SQL2k to be varchars, while my application was passing in an nvarchar field, and the blank Unicode bytes were throwing off the hash comparison.

        Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world. -Me explaining my job to an engineer

        H Offline
        H Offline
        Howard Richards
        wrote on last edited by
        #3

        Ouch.. at least mine were both varchars. It would be nice if the T-SQL parser would throw out a warning. PS. like the sig.

        'Howard

        P 1 Reply Last reply
        0
        • H Howard Richards

          Ouch.. at least mine were both varchars. It would be nice if the T-SQL parser would throw out a warning. PS. like the sig.

          'Howard

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          Howard Richards wrote:

          PS. like the sig.

          I agree. It is right on with what I have to deal with sometimes :)

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon

          1 Reply Last reply
          0
          • R Robert Royall

            I've had something similar happen with some user authentication work I was doing. I was generating an MD5 password hash and sending it to SQL Server 2000 for verification but even though I checked it for two hours, the application would never verify the password correctly even though it worked when I wrote out the SQL statement in Enterprise Manager. It turned out to be essentially a cast error - string literals in SQL statements are considered by SQL2k to be varchars, while my application was passing in an nvarchar field, and the blank Unicode bytes were throwing off the hash comparison.

            Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world. -Me explaining my job to an engineer

            B Offline
            B Offline
            Bassam Saoud
            wrote on last edited by
            #5

            Robert Royall wrote:

            Imagine that you are hired to build a bridge over a river which gets slightly wider every day; sometimes it shrinks but nobody can predict when. Your client provides no concrete or steel, only timber and cut stone (but they won't tell you what kind). The coefficient of gravity changes randomly from hour to hour, as does the viscosity of air. Your only tools are a hacksaw, a chainsaw, a rubber mallet, and a length of rope. Welcome to my world.

            wow man, I do not want to be you :laugh: :laugh:

            1 Reply Last reply
            0
            • H Howard Richards

              I was having a hard time figuring out why my T-SQL query wasn't working.

              DECLARE @billid int, @cli varchar;
              SET @billid =12345;
              SET @cli = '02077778888';

              SELECT COUNT(*) FROM Records WHERE BillID = @billid AND CLI = @cli;

              This query consistently returned a result of zero records, despite my knowing that records existed for this combination, e.g.:

              SELECT COUNT(*) FROM Records WHERE BillID = 12345
              AND CLI = '02077778888';

              would return the correct number. Solution: The length of the varchar had not been specified. Although it parses correctly it doesn't actually work without a length being specified. This simple change fixed things:

              DECLARE @billid int, @cli varchar(14);

              'Howard

              I Offline
              I Offline
              ian dennis 0
              wrote on last edited by
              #6

              It's not so much the length of the @cli varchar not being implicitly specified, it's the fact that @cli and CLI are different lengths. If you'd changed your second line to SET @cli = '02077778888vvv' (where v=space) it would have worked. I bet your second query wouldn't have worked as written (...WHERE BillID = 12345 AND CLI = '02077778888'). But I bet a generic SELECT (with no WHERE clauses) would have shown a row with (apparently) BillID = 12345 and CLI = '02077778888'. Actually, CLI would have been '020777788888 '

              H 1 Reply Last reply
              0
              • I ian dennis 0

                It's not so much the length of the @cli varchar not being implicitly specified, it's the fact that @cli and CLI are different lengths. If you'd changed your second line to SET @cli = '02077778888vvv' (where v=space) it would have worked. I bet your second query wouldn't have worked as written (...WHERE BillID = 12345 AND CLI = '02077778888'). But I bet a generic SELECT (with no WHERE clauses) would have shown a row with (apparently) BillID = 12345 and CLI = '02077778888'. Actually, CLI would have been '020777788888 '

                H Offline
                H Offline
                Howard Richards
                wrote on last edited by
                #7

                Can you explain how '02077778888vvv' would match with the database field value of '02077778888' ?

                'Howard

                I 1 Reply Last reply
                0
                • H Howard Richards

                  Can you explain how '02077778888vvv' would match with the database field value of '02077778888' ?

                  'Howard

                  I Offline
                  I Offline
                  ian dennis 0
                  wrote on last edited by
                  #8

                  In my previous post, I used 'v' to indicate a space ... a holdover from the old days where spaces were commonly indicated by using a kind of upside-down delta (▼) I'm suggesting that you inserted a row with CLI set to '02077778888' [eg "INSERT INTO Records (BillID, Cli) Values (12345, '02077778888')" ] but, because the CLI column was set to 14 characters, the field was actually stored as '02077778888▼▼▼' (3 spaces tacked on the end) So searching for '02077778888' didn't work, what you needed to do was search for '02077778888▼▼▼', which you did by dimming @cli as varchar(14). Does that make sense? I often get caught out by fields that are right-padded to the cell-width.

                  H 1 Reply Last reply
                  0
                  • I ian dennis 0

                    In my previous post, I used 'v' to indicate a space ... a holdover from the old days where spaces were commonly indicated by using a kind of upside-down delta (▼) I'm suggesting that you inserted a row with CLI set to '02077778888' [eg "INSERT INTO Records (BillID, Cli) Values (12345, '02077778888')" ] but, because the CLI column was set to 14 characters, the field was actually stored as '02077778888▼▼▼' (3 spaces tacked on the end) So searching for '02077778888' didn't work, what you needed to do was search for '02077778888▼▼▼', which you did by dimming @cli as varchar(14). Does that make sense? I often get caught out by fields that are right-padded to the cell-width.

                    H Offline
                    H Offline
                    Howard Richards
                    wrote on last edited by
                    #9

                    Thanks for the clarification. However I didn't use char - it was always varchar so it would not have spaces appended?

                    'Howard

                    I 1 Reply Last reply
                    0
                    • H Howard Richards

                      Thanks for the clarification. However I didn't use char - it was always varchar so it would not have spaces appended?

                      'Howard

                      I Offline
                      I Offline
                      ian dennis 0
                      wrote on last edited by
                      #10

                      I've seen cases (depending on specific implimentation of SQL) where a supposed varchar will pad. But it's only a suggestion - and easy enough to test, I'd think :)

                      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