o3d Posted May 16, 2007 Share Posted May 16, 2007 Good day, I need help to determine if a field in my where clause is in fact a valid integer value. I don't know how to do exception handling in a query so any help would be appreciated. I suppose the query could look something like: select cast(field2 as decimal) as field2 from table1 where is_number(field2) = 't' Quote Link to comment https://forums.phpfreaks.com/topic/51619-solved-validate-integer-value-in-query/ Share on other sites More sharing options...
the_oliver Posted May 19, 2007 Share Posted May 19, 2007 Not sure if i understood that but perhaps something simple like: SELECT cast(field2 as decimal) as field2 FROM table1 WHERE field2 > 0 Quote Link to comment https://forums.phpfreaks.com/topic/51619-solved-validate-integer-value-in-query/#findComment-257147 Share on other sites More sharing options...
o3d Posted May 30, 2007 Author Share Posted May 30, 2007 Thanks for the reply, but when you see if a value is > 0, I assume postgres also checks that it is not null. So nope, didn't work. --query select cast(a.field1 as decimal) from ( select '1' as field1 union select 'two' union select '3' ) as a where a.field1 > 0 --result ERROR: invalid input syntax for type numeric: "two" SQL state: 22P02 Quote Link to comment https://forums.phpfreaks.com/topic/51619-solved-validate-integer-value-in-query/#findComment-264500 Share on other sites More sharing options...
o3d Posted July 26, 2007 Author Share Posted July 26, 2007 I found a solution, don't know why i haven't looked at this before. I used a plain simple regular expression to determine if the value contains numeric values: select cast(a.field1 as decimal) from ( select '1' as field1 union select 'two' union select '3' ) as a where a.field1 ~ '^\\d+$' This query will only return fields which contains numeric values. RESULT ?column? -------- 1 3 Quote Link to comment https://forums.phpfreaks.com/topic/51619-solved-validate-integer-value-in-query/#findComment-307722 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.