views:

157

answers:

4

An error-free column name syntax is [My Column] instead of My Column which causes an error.

An error-free string value syntax is '25,00' instead of 25,00 which causes an error.

I'm getting an error using single quotes to enclose values, if the column data type is numeric. Are there any other ways to enclose values safely for string or numeric data types?

+2  A: 

Numeric values don't have any enclosures or comma's.

For strings, depending on your settings, in certain DB's it could be single or double quotes.

klabranche
A: 

A column name like My Column causes an error because of the space in it. [My Column] removes the ambiguity.

A value such as '25,00' is valid because the quotes make it a string, while 25,00 isn't a valid number (at least not in your part of the world) because of the comma.

If you were to insert 25,00 as a number, how is the DB able to distinguish it from two numbers?

pavium
+1  A: 

In SQL Server the Cast / Convert functions are regionally aware. Therefore use Convert in your query, passing the number as a quoted string, to convert it to the required decimal type. eg:

SELECT CONVERT(decimal(5,2),'1234,56')
Ash
I believe this is wrong. SQL Server never uses the regional settings to interpret the decimal separator, as far as I know. Did you in fact test this CONVERT statement on a European installation and find that it succeeds?
Steve Kass
+1  A: 

You are probably getting an error when you use quotes because the string '25,00' is not a valid decimal number. Check your RDBMS documentation to see how strings are implicitly converted to number types.

Without the quotes, 25,00 is also invalid, I believe, regardless of your location. The SQL standard does not permit literal numbers to be specified using comma as the decimal separator.

Steve Kass