Why does comparison of a where condition to a numeric value without quotations(char datatype) returns wrong records? RRS feed

  • Question

  • Hi,

    I have this table(3rd pic).....with data as follows(1 st pic)...

    My Question is when I try this below query(where [colname]=0)..two rows are coming up...but if i try (where [colname]='0')( with quotes).Only one row is showing up.can I know the reason behind it please?

    Thank u

    Monday, October 21, 2019 2:49 PM


All replies

  • "-" is the same as "-0". And as you're querying for 0 a type conversion happens. Thus two rows.

    p.s. from the given sample, your travel code column seems to make no sense. It should be NULL or 1..2. But nothing else. From the semantics, code and key seem to be also redundant.

    Monday, October 21, 2019 2:57 PM
  • Just to be clear.

    This is because of "implicit conversion" of the TravelModeCode to an integer value.  Your query results in:

    WHERE CAST([TravelModeCode] AS INT) = 0

    It is lucky all your values for TravelModeCode are able to be converted by implict conversion to integer and SQL Server converts '-' to 0 and does not error.  If you had other non-integer values, you would be getting an error "cannot convert 'a' to integer".

    You can see this by running:

    SELECT CAST([TravelModeCode] AS INT) as Int_TravelModeCode
    FROM ....

    Please see:

    • Marked as answer by Jennifer Zen Monday, October 21, 2019 5:57 PM
    Monday, October 21, 2019 3:32 PM
  • Hi,

    Thank you very much for the explanation and also after going through the article ,I was able to solve the issue with my stored proc by explicity casting the above field as varchar.



    Monday, October 21, 2019 6:04 PM
  • Hi,

    Thanks for your reply.

    Monday, October 21, 2019 6:05 PM
  • Numeric values do not have quote marks!! Where did you get this idea? Also, please stop posting pictures like a child who does not have words yet. Where is the DDL? Sample data? Then why did you use integer for a code? Codes are on a nominal scale, and not a ratio or interval scale. 

    --CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking in Sets / Trees and Hierarchies in SQL

    Monday, October 21, 2019 7:37 PM
  • Sir,
    Please calm down....

    --Numeric values do not have quote marks!! --
    I know numeric values don't have quotations-Read my question properly

    --Where did you get this idea? ---
    Ever heard of curiosity to test when you are learning something?? If you see ,the field is char datatype (from pics)

    ---Also, please stop posting pictures like a child who does not have words yet---
    Ever heard of this"Pictures Speak Louder Than Words"
    Looks like you are an author,u should have known better.

    --Where is the DDL? Sample data? Then why did you use integer for a code?--
    Cant u see the pics??

    --- Codes are on a nominal scale, and not a ratio or interval scale---
    I was given the dataset to work on..That's how it is...
    I already marked Tom Phillips's reply as an answer and I very much appreciate his help for explaining about sql conversions.
    People come here desperate to find an answer for something that they couldn't get on google search.
    Please do bear in mind that a person can be just a beginner and naive and trying to learn something..So rather than replying with all sort of weird questions..please help like others and if not just move on..
    No need to be rude ..


    Tuesday, October 22, 2019 5:29 PM
  • Good day Jennifer,

    I recommend to ignore CELKO as others do.
    He is a well know abuser who is bullying newbies IMO probably in order to show that he is better. I notice that he try to pick irrelevant points instead of providing answer to the question many times (I wonder if he know the answer in these cases).

    You can check the following thread where he complained that the spam engine marked his messages as spam. He did not even have the minimum honor of reviewing the responses he received and answer them or closing the thread, which is probably one of the most rude actions someone can do in the forum! I hate it when someone open a thread and do not come back to it.

    Here you can see his "ability" to write code when he actually try to provide answer:

    This simple and so short code which he provided includes multiple mistakes and it will raise error if you will try to execute it. For example (1) using ROW_Number as it was a variable instead of function (2) using where condition on "t" while t is not the column's name but alias (3) Using where condition on row_number function (issue 2 and 3 could be avoid by using CTE)...

    CREATE VIEW Top_three_foobar (foobar, foobar_top)
       FROM Floob
    WHERE t <= 3);
    In short, I recommend simply to ignore and not to open a fight in the forum😀

    signature   Ronen Ariely
     [Personal Site]    [Blog]    [Facebook]    [Linkedin]

    Wednesday, October 23, 2019 5:57 AM
  • Hi Ronen,

    Thank you very much for your kind and supporting response. I really appreciate people like you, who takes time to help us naive people in understanding the problem. I assure you, I never want to pick up a fight with anyone on internet, let alone this forum. :)

    It really feels bad when someone is being rude to you when your learning something.It creates a fear to ask another question hoping that it can be a dumb one and someone can get offended by it.

    I pray for the person who gets him as a senior/manager..

    Regarding his query,he could have also used that select statement as a subquery to get the records from and putting that where clause out of the window .(Ofcourse treating partition as a function)

    But anyways thank very much for your response.I appreciate it :)



    • Edited by Jennifer Zen Wednesday, October 23, 2019 3:49 PM
    Wednesday, October 23, 2019 3:16 PM