Solved

SQL - put $ in front of field

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
migration MS SQL database to Oracle 30 60
sql server computed columns 11 31
SQL Server 2012 r2 - Varible Table 3 23
SQL - Update field defined as Text 6 16
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

776 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