Both cast and convert functions in MySQL throw 'Truncated incorrect INTEGER value' when casting a string to an integer value:
select cast(mycolumn as unsigned) from mytable;
I would like to get the behavior of PHP and most other programming languages, which means I would like to cast all non-numeric strings to zero.
What is the most efficient way to do this in MySQL?
int x = "Hello";in C or C# orint("Hello")in Python and see what you get. A lot of programming languages are statically typed or don't allow invalid type conversions. You might spend all day in PHP and JavaScript, but that doesn't mean dynamically typed languages that silently modify data when you make a bad type conversion are the default.atoiand some related functions in C/C++.CAST()andCONVERT()are. A C typecast would be(int) 'Hello'.atoihas a special algorithm to intentionally throw data away.