how to get 2 digits after decimal point in tsql? how to get 2 digits after decimal point in tsql? sql-server sql-server

how to get 2 digits after decimal point in tsql?


Try this one -

DECLARE @i FLOAT = 6.677756SELECT       ROUND(@i, 2)    , FORMAT(@i, 'N2')    , CAST(@i AS DECIMAL(18,2))    , SUBSTRING(PARSENAME(CAST(@i AS VARCHAR(10)), 1), PATINDEX('%.%', CAST(@i AS VARCHAR(10))) - 1, 2)    , FLOOR((@i - FLOOR(@i)) * 100)

Output:

----------------------6,686.686.686767


You could cast it to DECIMAL and specify the scale to be 2 digits

decimal and numeric

So, something like

DECLARE @i AS FLOAT = 2SELECT @i / 3SELECT CAST(@i / 3 AS DECIMAL(18,2))

SQLFiddle DEMO

I would however recomend that this be done in the UI/Report layer, as this will cuase loss of precision.

Formatting (in my opinion) should happen on the UI/Report/Display level.


Try cast result to numeric

CAST(sum(cast(datediff(second, IEC.CREATE_DATE, IEC.STATUS_DATE) as float) / 60)    AS numeric(10,2)) TotalSentMinutes

Input

1
2
3

Output

1.00
2.00
3.00