View Single Post

   
  #1 (permalink)  
Old 04-24-2008, 07:09 PM
incuso
 
Posts: n/a
Default How to not repeat functions?

I am extensively using string functions on a table fields to extract
suitable data.

Unfortunately, I do not understand how to avoid to repeat them (that I
assume is wasting CPU cycles).

Just an example

SELECT * FROM a bunch of tables WHERE other conditions AND
SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1) BETWEEN
X AND Y OR SUBSTRING_INDEX(REPLACE(REPLACE(peso,",","."),"
g",""),"-",1) BETWEEN X AND Y OR
(SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1)<X
AND SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".")," g",""),"-",1)>Y)

Ok, ok there is an error in the function usage, but this is not the
point, it is only an example.

You can see that "SUBSTRING_INDEX(REPLACE(REPLACE(peso,",",".") ,"
g",""),"-",1)" is reapeted 4 times, is there any way to use it only
once?

Thanks!
M.
Reply With Quote