Link to home
Start Free TrialLog in
Avatar of Bharat Guru
Bharat Guru

asked on

How to convert decimal to varchar

Select OrderID,
'Sell--' + Convert(varchar(24), sum(CAST(Price AS DECIMAL(13,2))) )
From OrderDetail
GroupBy OrderID

I want to display price as varchar but somehow above code send an following error
"Error converting data type varchar to numeric."

I want to display result as following
OrderID Price
-----------------
1, S-212.23
2, S-46.83
3, S-912.20
4, S-52.73
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland image

try
sounds like you've got some invalid data... what is the datatype of the price column?

 Select OrderID,
'Sell--' +Convert(varchar(24),sum(case when isnumeric(price) when 1 then convert(decimal(13,2),Price))
                                 else 0.00 end))
 From OrderDetail
GroupBy OrderID

and use this to identify any non numerics..
select orderid , price
from orderdetail
where isnumeric(price) = 0
Avatar of Bharat Guru
Bharat Guru

ASKER

I actually want to do something like following

Declare @Temp CHAR (1)
Set @Temp = 'Y'

Select OrderID,
              Case When @Temp = 'Y' Then
                  Convert(varchar(24), sum(CAST(Price AS DECIMAL(13,2)))  )
              Else
                  sum(CAST(Price AS DECIMAL(13,2)))
              End AS "Price"

 From OrderDetail
GroupBy OrderID


Price is a varchar field in table
ASKER CERTIFIED SOLUTION
Avatar of Lowfatspread
Lowfatspread
Flag of United Kingdom of Great Britain and Northern Ireland 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