table field datatype

What datatype should I use for a field in my sql server table that should be able to hold values such as:
-1.092
0
2.9875645
25436.432567

etc.
Thanks



Answer this question

table field datatype

  • Kannan.B

    If you want save numbers with floating point, you should use real.

    If you want to save numbers with fixed point, you shoul use decimal(12,7) (total <=12 digits and after dot <=7 digits)


  • Alexei_shk

    Okay, I thought this answered my question as well, but when I use the decimal(18, 4) data type, and try to use 7.99 as a value, I get an invalid value error. What am I doing wrong I should mention that at this point I am inputing the value directly into the table data from C#.Net. Is it simply a matter of format
  • ledwinka

    As you can see the decimal placces will vary.

    Does this mean I have to use floats instead of decimals
    Thanks


  • SimonOng

    First, thanks for replying, and apparently for putting some real effort into your response as well!

    Anyway, to answer your question "Do you use SqlCommand with Parameters or build query by string concatenation ", neither.

    I created the table using the database designer embedded into C# .net (express). I set the precision and scale as (18, 4). Then, because I wanted some simple test data, I chose "show table data" by right clicking on the table and started punching in data. First few fields no problem, but when I tried to put in 7.99 in my decimal (18,4) field, nada. I tried using a comma instead of a decimal. I even tried putting parenthesis around it just for the heck of it. Heh, this wasn't covered in the "absolute beginners guide to C#", but I'm bound and determined to learn this stuff. Thanks for bearing with!


  • Detisch

    Do you use SqlCommand with Parameters or build query by string concatenation

    Following code works:

    --Create table

    create table decimal_test (

    id int identity not null primary key ,

    d decimal(18,4)

    )

    --C#

    SqlConnection conn = new SqlConnection("Server=.;DataBase=Demo;Integrated Security=SSPI");

    conn.Open();

    SqlCommand cmd = new SqlCommand("insert into decimal_test values(@d)", conn);

    cmd.Parameters.Add("@d", System.Data.SqlDbType.Decimal);

    cmd.Parameters["@d"].Value = 7.99;

    int res = cmd.ExecuteNonQuery();

    Console.WriteLine(res.ToString());

    But if i use string concatenation, I get error. Following code doesn't work, because in my culture setting decimal separator is a "," (comma):

    SqlConnection conn = new SqlConnection("Server=.;DataBase=Demo;Integrated Security=SSPI");

    conn.Open();

    decimal d = 7.99M;

    SqlCommand cmd = new SqlCommand("insert into decimal_test values("+d.ToString()+")", conn);

    int res = cmd.ExecuteNonQuery();

    Console.WriteLine(res.ToString());


  • AndyPham

    decimal(12,7) could store values with 0,1,2,3,4,5,6,7 decimal places, but total number of digits must be less or equals to 12.

    Main difference between float(real) and decimal, float is approximate-number data type, decimal is exactly datatype. As result, storing money data in float is a bad idea.


  • table field datatype