Is there any way to cast a number into a decimal with a specified number of decimal places? I tried:
SELECT ...
CAST(NumericField AS NUMERIC(15, @DecimalPlaces) AS NumericField,
...
But it didn't work.
EDIT: I made a mistake and wrote NUMBER
instead of NUMERIC
. But the question stands still: How do I cast to a NUMERIC
with a specified number of decimal places?
declare @Value float = 123.4567, @RoundTo int = 2
select round(@Value * power(10, @RoundTo), 0) / power(10, @RoundTo)
declare @decimal int=5
declare @decimalNum float =8931.0380106023125083
select ROUND(@decimalNum, @decimal,1)
For trailing zeros use this:
declare @decimal int=5
declare @decimalNum float =8931.12
select STR(@decimalNum, 25, @decimal)
Please note, the above select will return a varchar type, not decimal, numeric, float or any other types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With