Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I quote numbers in SQL?

Tags:

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 can't find that quotation and I need to refresh my memory to see if I should quote numbers.

like image 237
ambiguousmouse Avatar asked Jan 16 '10 21:01

ambiguousmouse


1 Answers

You should not quote numbers if you want to treat them as numbers.

You're correct by remembering that it makes it a string.

SELECT 10 AS x 

is perfectly legal and will return (in most database engines) a column of datatype int (or a variation thereof.)

If you do this:

SELECT '10' AS x 

instead you'll get a textual data type. This might too be suitable in some cases, but you need to decide whether you want the result as text or as a number.

like image 108
Lasse V. Karlsen Avatar answered Sep 18 '22 23:09

Lasse V. Karlsen