Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have some sales numbers in a string column that I need to convert to some format so that i can calculate them with each other but I get this error while trying to convert them.

Conversion failed when converting the varchar value '-6.353,35' to data type int.

I'm not allowed to lose any money by rounding it up. It doesnt mather but in what type i convert as long as im not rounding them up. What's your thoughts?

For example i have -6.353,35 and 300,30 and i want to sum them too -6.053,05

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
437 views
Welcome To Ask or Share your Answers For Others

1 Answer

try this...

select convert(int,convert(float,replace('-6.353,35',',','')))

as there are (,) Commas it cannot be converted to float,so remove the (,)commas after converting to float we can convert to int

If you want decimal values, then you should use float

select convert(float,replace('-6.353,35',',',''))

Edit Like @marc_s suggested, it is preferred to use decimal rather than float

select convert(decimal,replace('-6.353,35',',',''))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...