[sql] Is there a way to cast float as a decimal without rounding and preserving its precision?

So it appears that if you

CAST(field1 as decimal) field1

this will automatically add rounding.

The original is defined as:

field1 type:float length:8 prec:53

I need to cast it to decimal, because I need my Entity Framework layer to generate this field as decimal (instead of double).

Is there a way to cast it as decimal, so that it preserves original precision, and doesn't round?

I would like to avoid having to declare the precision in the cast, because:

  1. there are 100's of fields involved with varying precision, and;
  2. if the underlying table changes in the future, it could cause unforeseen bugs to emerge, and;
  3. makes the management more difficult.

This question is related to sql sql-server casting

The answer is


cast (field1 as decimal(53,8)
) field 1

The default is: decimal(18,0)


Try SELECT CAST(field1 AS DECIMAL(10,2)) field1 and replace 10,2 with whatever precision you need.


Examples related to sql

Passing multiple values for same variable in stored procedure SQL permissions for roles Generic XSLT Search and Replace template Access And/Or exclusions Pyspark: Filter dataframe based on multiple conditions Subtracting 1 day from a timestamp date PYODBC--Data source name not found and no default driver specified select rows in sql with latest date for each ID repeated multiple times ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database

Examples related to sql-server

Passing multiple values for same variable in stored procedure SQL permissions for roles Count the Number of Tables in a SQL Server Database Visual Studio 2017 does not have Business Intelligence Integration Services/Projects ALTER TABLE DROP COLUMN failed because one or more objects access this column Create Local SQL Server database How to create temp table using Create statement in SQL Server? SQL Query Where Date = Today Minus 7 Days How do I pass a list as a parameter in a stored procedure? SQL Server date format yyyymmdd

Examples related to casting

Subtracting 1 day from a timestamp date Cast object to interface in TypeScript TypeScript enum to object array Casting a number to a string in TypeScript Hive cast string to date dd-MM-yyyy Casting int to bool in C/C++ Swift double to string No function matches the given name and argument types C convert floating point to int PostgreSQL : cast string to date DD/MM/YYYY