SQL, if field length is less than 10 characters, search through as without %%

How can I do this, if my db field is less than 10 characters, search through mild, but if more than 10, than with %% parameters? So how do you check the length of a field?

My ruby ​​code is like this:

@search = CrossList.find(:all, :conditions => ['cross_value like ? ', oem_condition]) 

      

But how to do something like:

@search = CrossList.find(:all, :conditions => ['length(cross_value) < 10 and cross_value like ? ', oem_condition]) 

      

yet

 @search = CrossList.find(:all, :conditions => ['length(cross_value) >= 10 and cross_value like %?% ', oem_condition]) 

      

+3


source to share


1 answer


Use two conditions and OR

them together:

@search = CrossList.find(:all, :conditions => [
   "(length(cross_value) >= 10 AND cross_value like CONCAT('%',?,'%')) OR (length(cross_value) < 10 AND cross_value like ?) ",
    oem_condition,
    oem_condition
]) 

      



The condition LENGTH()

must be met in one of two groups ()

, and depending on what is met, it will also be required that the condition LIKE

with which it AND

'd be applied.

+2


source







All Articles