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

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

sql - Conversion fails when trying to Convert string to int

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

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

1 Answer

0 votes
by (71.8m points)

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',',',''))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...