Subject Re: [firebird-support] How to CAST float to integer with error?
Author liviuslivius
Hi,
 
this is simple with "case" or "iif"
 
this run ok
SELECT CASE WHEN CAST('123.00' AS INTEGER)<>CAST('123.00' AS NUMERIC(18, 3)) THEN CAST('PRECISION LOST' AS INTEGER) ELSE CAST('123.456' AS INTEGER) END FROM RDB$DATABASE
 
this run with an error "conversion error from string "PRECISION LOST FOR 123.456""
 
SELECT CASE WHEN CAST('123.456' AS INTEGER)<>CAST('123.456' AS NUMERIC(18, 3)) THEN CAST('PRECISION LOST FOR 123.456' AS INTEGER) ELSE CAST('123.456' AS INTEGER) END FROM RDB$DATABASE
 
regards,
Karol Bieniaszewski
 
 
W dniu 2015-05-25 12:04:09 użytkownik brucedickinson@... [firebird-support] <firebird-support@yahoogroups.com> napisał:
 

Hello,

is it possible by using CAST or in any other way to get database error when casting such number to integer?

SELECT CAST('13.245' AS INTEGER) FROM RDB$DATABASE

This gives 13 but instead I would like to get an error because precision is lost.

However, I would like to NOT get an error when doing this:

SELECT CAST('13.000' AS INTEGER) FROM RDB$DATABASE

Becasue here precision is not lost.

Is it possible to obtain such behaviour in easy way or do I have to use regular expressions?


Best regards.