Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to adjust decimal places in sql query

how can I make this output:

Total
      62809.44
        91686.34
        70671.29

to be

 $62.81
    $91.69
    $70.67

i want mo move the decimal places to the left 3 times, round and then add a character. thanks

like image 797
404notfound Avatar asked Dec 03 '25 07:12

404notfound


2 Answers

Try following

select '$' + CONVERT(NVARCHAR(100), ROUND(70671.29/1000,2))

If you want to remove trailing zeros, try following.

select '$' + CONVERT(NVARCHAR(100),CAST( ROUND(70671.29/1000,2) AS DECIMAL(5,2)))
like image 186
tchelidze Avatar answered Dec 05 '25 21:12

tchelidze


SELECT
    '$' + FORMAT(v / 1000, 'n2'), -- 2012+
    '$' + LEFT(CAST(v / 1000 AS DECIMAL(18,2)), 20) -- 2005+
FROM (
    VALUES
        (62809.44),
        (91686.34),
        (70671.29)
) t(v)

Output -

---------
$62.81   
$91.69   
$70.67   
like image 20
Devart Avatar answered Dec 05 '25 20:12

Devart