Skip to main content

Is there a way to test if a varchar is cast-able into a int before casting it?

  • January 9, 2014
  • 1 reply
  • 6 views

David_Chen
Forum|alt.badge.img+1
Is there a way to test if a varchar is cast-able into a int before casting it? select (int '3'); works. But I don't know what values I will be getting; I just know that they are supposed to be integers. What I need is something like: select case when isInteger('4') is true then cast('4' as int) else null end as mycolumn;

1 reply

Peter_Sheridan
  • New Participant
  • January 9, 2014
During imports I'll typically use regexp_like for this and check for non-digit characters, e.g.
select regexp_like('abc', '[^0-9]');
true
select regexp_like('1234', '[^0-9]');
false