Casting text to a length that is shorter than the value results in truncated text.
select
cast('ROAD-1234' as varchar2(8)) as id
from
dual
ID
--------
ROAD-123
--^ Notice that the number 4 has been removed
I would have thought that CAST AS VARCHAR2
would have behaved similarly to CAST AS NUMBER
.
select
cast(1234 as number(3)) as id
from
dual
Error: ORA-01438: value larger than specified precision allowed for this column
Why does CAST AS VARCHAR2 silently truncate text instead of throwing an error like CAST AS NUMBER does?