Solved

SQL - put $ in front of field

Posted on 2013-05-29
2
361 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

718 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