Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

SQL query output only need two decimal places

select  convert(varchar,totalamt ) as TotalAmount   from table1

Output:  18882.019

15882.050
8078.816

I need the output in the following format  (need only two decimal places). How do i achieve this ?
18882.01
15882.05
8078.81
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

SELECT CAST(totalamt AS DECIMAL(9,2)) AS TotalAmount
FROM table1

OR

SELECT ROUND(totalamt, 2) AS TotalAmount
FROM table1
Avatar of Varshini S
Varshini S

ASKER

Brain,
I need the output in varchar
I first question is "Why?" since this should be handled at the presentation layer but just wrap it in a CONVERT/CAST to varchar.

SELECT CAST(ROUND(totalamt, 2) AS VARCHAR) AS TotalAmount
FROM table1
I am exporting the output to CSV.  So the column should be string.
I have used the below script. But the output still showing the third decimal 0

SELECT  CAST(ROUND(totalamt, 2) AS VARCHAR)   AS   TotalAmount
FROM table1


Output:
834.200
3015.380
ASKER CERTIFIED SOLUTION
Avatar of Brian Crowe
Brian Crowe
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
if it is available to you (version SQL 2012 onward) try FORMAT()

e.g.

FORMAT(totalamt,'0.00')