Should I quote numbers in SQL?

后端 未结 7 1567
逝去的感伤
逝去的感伤 2020-12-06 16:21

I remember reading about quoting stuff when doing a SQL query and that when you quote something, it becomes a string. I also read that numbers should not be quoted. Now, I c

7条回答
  •  心在旅途
    2020-12-06 17:01

    Here's an example, where quoting would produce inconsistent results (in MySQL):

    select 1 < 1.0;      // returns 0
    
    select '1' < '1.0';  // returns 1
    

    That's because the second comparison is performed using the current string collation rather than numerically.

    It's better not to quote numbers, since that would just be an extra unnecessary step for the database to convert the string literal into a numeric value for comparison and could alter the meaning of comparisons.

提交回复
热议问题