I'm trying to cast a value to covert from string to integer or numeric
The string I'm trying to cast is like this: production-ross-3
but when I run SELECT CAST('production-ross-3' as numeric) it returns Invalid NUMERIC value: production-ross-3
why? it's supposed to cast from string to numeric
I'm trying to cast a value to covert from string to integer or numeric
The string I'm trying to cast is like this: production-ross-3
but when I run SELECT CAST('production-ross-3' as numeric) it returns Invalid NUMERIC value: production-ross-3
You have incorrectly assumed that cast will (in addition to its stated task of converting types) locate any digit in the given string. However it just does not do that and as per the warnings given it produced a runtime error.
To achieve the wanted extraction of digits from the string you can use:
So, to achieve the overall wanted outcome you can combine these e.g:
REGEXP_EXTRACT takes two arguments: the string to extract from, and a regular expression that defines the pattern to match. Here r'\d+' is a regular expression that matches one or more digits in the string. Ref:
nb casting strings to any form of number or date is fraught with difficulty and runtime error is not uncommon. You can use SAFE_CAST to avoid those runtime errors but this simply returns NULL if it cannot make the type conversion.