usumoio
usumoio

Reputation: 3568

MySQL Cast operation throws generic error

This statement is part of a larger statement but this past is not work. It throws the usual 1064 error. The goal is to parse a string to get at the number that is in the middle of it, then to cast this to an int so that said number can be compared against other id values. This is the failing query

SELECT CAST(REPLACE(REPLACE('remove_this-151-remove_this_too', 'remove_this-', ''), '-remove_this_too', '') as INTEGER);

I'm not sure why this type of syntax is not allowed. Thank you to anyone who can help.

Upvotes: 2

Views: 91

Answers (1)

Alma Do
Alma Do

Reputation: 37365

You need to do that with:

SELECT CAST(REPLACE(REPLACE('remove_this-151-remove_this_too', 'remove_this-', ''), '-remove_this_too', '') as UNSIGNED)

-since there's no INTEGER modifier for CAST() in MySQL, but there are SIGNED and UNSIGNED

Upvotes: 2

Related Questions