Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...