I pull decimals from a SQL 2000 database that have a scale of (14, 6). Let's say the value from SQL is 0, so it comes out 0.000000.
I then do the following comparison:
decimal x = 0;
Convert.ToDecimal(column["column name"]) == x
it comes back false. Why ! How can I compare a SQL decimal with a scale set to a local decimal

Comparing sql decimal to .net decimal
Bradley Reynolds
That's what I do:
private
string columnToString(object column, Type type){
if (column == DBNull.Value){
return "(null)"; } else if (type == typeof(string)){
return column.ToString(); } else{
if (type == typeof(decimal)){
return Convert.ToDecimal(column).ToString("N6");}
return Convert.ChangeType(column, type).ToString();}
}
What about decimals with a scope of 8 12
Maybe the decimal should be converted to a double or float for the comparison I need to check if that will cause any loss of data or other issues.
What if you fed the value into a command parameter with the scope set and compared the value property
I have seen where you can add scoped decimals to a dataset using SQL Server 2005 or .Net 3.0 or something. I guess that will be the true solution to this problem.
GSReddy
try formatting the scale of your variable to 6 decimal places like the one the column field has.
Table.Columnname.ToString == X.ToString("N6")
AlucardHellSing
0 would not be equal to 0.0000 because you used == as your condition. == means that the two values you are comparing should be EXACTLY equal. It is better to convert both into the same format so that you can have accurate results in comparisons. you can add something like this inside your loop
if (IsDecimal(column["name"]) == true)
column["name"].ToString("N6") == column["name"].ToString("N6")
else
column["name"] == column["name"]
Francesco De Vittori
Thanks for the reply!
Well, that's what I did and it works, but I was hoping there is a better way.
I am writing a program that loops through the columns in different tables and finds changes.
With every other type, I can simply do column["name"] == column["name"]. It really stinks that I have to write extra code to handle decimals. Plus, the decimals have different scales!
Could anyone explain why 0 does not equal 0.0000
Should I convert it to another type
Is there any way to do a comparison using Sql Types