Reputation: 119
I get the error of "format or data contains a bad character" from the query below. The datatype of column1 is VARCHAR(2000)
. This is an example of the string 'ROC 2236 00 ROC'
for column1 and the result I would like to get is '2236.00'
SELECT ORDER_NUMBER,
CASE WHEN COLUMN1 LIKE 'ROC%' THEN CAST(OREPLACE(TRIM(COALESCE(SUBSTR('COLUMN1',5, 8),0)),' ','.' ) AS DEC(12,2) END
FROM TABLE1
WHERE CAST(COLUMN1 AS DEC(12,2)) > 0
Upvotes: 0
Views: 37569
Reputation: 81
I found using the function TRIM
to work better. I think the error is a result of leading/trailing spaces.
Upvotes: 1
Reputation: 61
It is hard to test this without actual data, however at first glance, you are trying to convert the word column1 to a decimal...
and your where clause is doing a straight conversion and with sample data provided it would not like converting "ROC" to a decimal
try
SELECT
ORDER_NUMBER
, CASE WHEN COLUMN1 LIKE 'ROC%' THEN
CAST(OREPLACE(TRIM(COALESCE(SUBSTR(COLUMN1,5, 8),0)),' ','.' ) AS DEC(12,2))
END
FROM TABLE1
WHERE CAST(OREPLACE(TRIM(COALESCE(SUBSTR(COLUMN1,5, 8),0)),' ','.' ) AS DEC(12,2)) > 0
Upvotes: 1