Solved

SQL - put $ in front of field

Posted on 2013-05-29
2
359 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.

734 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