Link to home
Start Free TrialLog in
Avatar of matija_
matija_Flag for Croatia

asked on

How to insert decimal number with "INSERT INTO"

Fellow Experts,

I'm using the following SQL query for inserting into MySQL database:

<%
Id = "8059"
Discount = "1,234"

SQL = "INSERT INTO tbl (fld1, fld2) VALUES ('" & Id & "', " & Discount & ")"
Conn.Execute(SQL)
%>

Open in new window


How to insert decimal number into database? The script returns "Column count doesn't match value count at row 1" (it obviously thinks I'm trying to insert another value after comma in decimal number)

Notes:
- I don't want to use RS.Open, RS.AddNew, RS.Update procedure due to speed benefits of INSERT method.
- "Id" and "Discount" values are populated from another table and I have thousands of those values which I want to insert into this "tbl" so they need to stay in variables.
- "fld1" is VARCHAR(50), "fld2" is DECIMAL(18,4)

Any help appreciated, Google was not my friend this time :(
Avatar of celazkon
celazkon
Flag of Czechia image

I would recomend to either change the datatype in the DB or to use the replace function for the discount variable:
discount = Replace(discount,",",".")
my guess would be, numbers don't contain commas.

Discount = "1234"

If other country formatting and that comma is supposed to be a decimal,

Discount = "1.234"

Looks like you are missing your other set of apostrophes around your Discount variable.

VALUES ('" & Id & "','" & Discount & "')"

As for forcing your database to display 2 decimal places, you need to make your field a decimal field.  Something like this would work:   decimal(10,2)  --10 is the total length of the field and the 2 is the number of decimal places to show.  This may seem a bit confusing, depending on how you are working with your MySQL DB.  Are you using MyPHPAdmin?  

If your field is a varchar (text), then you can format your number before inserting:  FormatNumber(variable, 2).

For example: VALUES ('" & Id & "','" & FormatNumber(Discount, 2) & "')"

FormatNumber is a ASP function.
Avatar of matija_

ASKER

@celazkon and @jrm213jrm213
is there a way not to replace with comma? I really don't want to use dots. My locale is always set up for "," to be decimal separator, no matter of country setting.

@Slim81
fields are already formatted: "fld1" is VARCHAR(50), "fld2" is DECIMAL(18,4)
Placing another set of apostophes would return an error.
ASKER CERTIFIED SOLUTION
Avatar of jrm213jrm213
jrm213jrm213
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of matija_

ASKER

@jrm213jrm213
Hm, how come when I update the decimal field with RS.Update, it writes number with comma to MySQL into the same field where INSERT INTO doesn't?
You will need to have functions similar to

function translate_decimal_for_display(val)
{
      //format as needed for display and return
}

function traslate_decimal_for_storage(val)
{
     //format as needed for mysql storage and return
}

and use them as needed
Avatar of matija_

ASKER

I just tested with "INSERT INTO tbl (fld1, fld2) VALUES ('" & Id & "', " & Replace(Discount, ",", ".") & ")" and it seems MySQL transforms it automatically to "," again when inserted :) Great. Thanks
does it store it in the database that way? As far as I know MySQL requires it to be a period.

http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html
Avatar of matija_

ASKER

It stores it in database with comma, although, it is being replaced with dot prior inserting. I check the values with MySQL Administrator. Maybe because of cp1250 database locale?
generally, the decimal value needs to be always with . sign
The stored data are always like that, but since you use different formating for displaying the data, what you see displayed is converted according to the locale settings.
e.g. the number is in database table's column stored lik 1.2345 but when you select the column, it's displayed according to the locale, in your case as 1,2345