Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace or remove characters and then cast to decimal

Tags:

sql

sql-server

I need to remove or replace an unwanted character (%) which is at the end of a string and then to cast the string to a decimal, because I need to get the sum of 2 fields named discount1 and discount2

The last combination I tried is:

replace ((p16.pa_value,'%','')CAST (p16.pa_value AS DECIMAL (3,0))) as discount2,

It seems to be super wrong as the CAST is inside of the replace statement

like image 579
Slim Avatar asked Oct 29 '25 07:10

Slim


2 Answers

try this:

CAST (replace (p16.pa_value,'%','') AS DECIMAL (3,0)) as discount2
like image 164
Joe G Joseph Avatar answered Oct 30 '25 22:10

Joe G Joseph


OK, I don't know why do you have three digits after decimal: DECIMAL (3,0) because if you convert to decimal it should be: decimal (10; how many zeros after ,) - more info here

Try this:

(cast(replace(p16.pa_value, '%','')  as decimal (10,2))) as discount
like image 30
Kamila Avatar answered Oct 30 '25 21:10

Kamila