I have a problem in a postgresql query


I want to do a filtering by ID number but this is a numeric type and the like does not accept it as I can do? the query is this "SELECT * FROM cliente WHERE identif LIKE '_%'"; where identif is of type numeric

asked by Efrainrodc 21.12.2016 в 01:38

1 answer


You can convert the field to a varchar using a CAST to be able to use the operator LIKE :

select *
  from cliente
 where cast(identif as varchar) LIKE '123%'


Sadly, if you had an index defined for the identif column, the type conversion will prevent the index from being used for better performance.

If performance is a problem, it may be worth changing the type of the column to be alphanumeric at one time. Since it is a cedula, it is not necessary that the field be numeric, since you do not need to perform arithmetic. An alphanumeric field makes more sense in your case.

answered by 21.12.2016 / 01:50