Ella McGregor
Ella McGregor

Reputation: 1

Conversion failed when converting the varchar value '%' to data type int SQL

a.taxamount is between 0.0 and 20.0, and I would like to display it as 0.0% or 120.0% but I am getting a conversion error.

SELECT....
,(a.taxamount * 100) + '%' AS TaxAmount

Upvotes: 0

Views: 214

Answers (2)

Mano
Mano

Reputation: 788

Try the following query.

SELECT CAST((a.taxamount * 100) AS VARCHAR(10))+ '%' AS TaxAmount

Upvotes: 0

Martin Smith
Martin Smith

Reputation: 453067

The + operator is used both for addition and concatenation. If you mix strings and numbers addition is assumed and it tries to implicitly cast the string to a number.

As you are on SQL Server 2008 you should convert the number to varchar first before concatenating.

CAST(a.taxamount * 100 AS VARCHAR(10)) + '%'

On more recent versions you could use the CONCAT function that does this implicitly or use FORMAT(a.taxamount, 'P1') to multiply by 100 and append the percent character.

Upvotes: 4

Related Questions