Solved

How to convert decimal to varchar

Posted on 2004-08-24
3
15,190 Views
Last Modified: 2008-01-09
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
0
Comment
Question by:Bharat Guru
  • 2
3 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11885013
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
0
 

Author Comment

by:Bharat Guru
ID: 11885488
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
0
 
LVL 50

Accepted Solution

by:
Lowfatspread earned 20 total points
ID: 11886399
you can only do this...
you can't have multiple datatypes for a column....

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

Select OrderID,
              Case When @Temp = 'Y' Then
                  Convert(varchar(24), price)
              Else
                  convert(varchar(13),Price)
              End AS "Price"
 From ( select orderid,sum(cast(price as decimal(13,2))) as  price from OrderDetail
GroupBy OrderID) as x


you could use dynamic sql (or have 2 sql statements (preferred solution))

declare @sqlstm varchar(8000)
Set @sqlstm='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 + ' from orderdetail group by orderid'

exec(@sqlstm)  


0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get column names from table in vb.net 8 26
SSRS 2013 - Overlapping reports 2 19
always on switch back after failover 2 31
Update a text value in another table 10 37
Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now