Skip to content
Advertisement

Should I quote numbers in SQL?

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.

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement