Solved

SQL - put $ in front of field

Posted on 2013-05-29
2
352 Views
Last Modified: 2013-05-29
I'm writing a case statement and I want either a $ to appear or % based on a value in a field.  But I get an error - "Error converting data type varchar to numeric.

How do I resolve?

SELECT Amount, 
CASE 
	WHEN id.DiscountType = 1 THEN 
		'$' + id.DiscountValue 
	ELSE 
		id.DiscountValue + '%' 
END AS DiscountValue2,
AppliedDate
From MyTable

Open in new window

0
Comment
Question by:CipherIS
2 Comments
 
LVL 75

Accepted Solution

by:
Aneesh Retnakaran earned 250 total points
ID: 39205178
SELECT Amount,
CASE
      WHEN id.DiscountType = 1 THEN
            '$' + cast(id.DiscountValue  as varchar )
      ELSE
            cast (id.DiscountValue as varchar)  + '%'
END AS DiscountValue2,
AppliedDate
From MyTable
0
 
LVL 20

Assisted Solution

by:dsacker
dsacker earned 250 total points
ID: 39205183
You can append a string to a numeric value. You'll have to convert DiscountValue, like so:
SELECT Amount, 
CASE 
	WHEN id.DiscountType = 1 THEN 
		'$' + CONVERT(varchar(20), id.DiscountValue)
	ELSE 
		CONVERT(varchar(20), id.DiscountValue) + '%' 
END AS DiscountValue2,
AppliedDate
From MyTable

Open in new window

0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

705 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

20 Experts available now in Live!

Get 1:1 Help Now