Why does casting this result of REGEXP_SUBSTR()
to a DECIMAL fail?
SELECT
REGEXP_SUBSTR('Cost (-$14.18)', '(?<=Cost [(]-[$])[0-9.]+') AS _extracted,
CAST(REGEXP_SUBSTR('Cost (-$14.18)', '(?<=Cost [(]-[$])[0-9.]+') AS DECIMAL(8,2)) AS cost_1,
CAST((SELECT _extracted) AS DECIMAL(8,2)) AS cost_2,
CAST((SELECT _extracted) * 1 AS DECIMAL(8,2)) AS cost_3,
CAST('14.18' AS DECIMAL(8,2)) AS cost_4;
+------------+--------+--------+--------+--------+
| _extracted | cost_1 | cost_2 | cost_3 | cost_4 |
+------------+--------+--------+--------+--------+
| 14.18 | 14.00 | 14.00 | 14.18 | 14.18 |
+------------+--------+--------+--------+--------+
Casting a plain string as in cost_4
seems to work. Multiplying the REGEXP_SUBSTR()
result by 1
also appears to work. But simply casting the result as I've done with cost_1
and cost_2
fails to produce the correct fixed point version of _extracted
.
Oddly, in my application using the backreference as I've done for cost_2
actually produces the correct result. Was unable to reproduce elsewhere but thought it worth mentioning.