Technical
String Manipulations Using Sql

gsub Using sql

I had a case where the string was not supposed to show a suffix. String looked like “GOWANUS-ZK-1” and I had to loose ”-1” part. Of course right away I flexed my regex muscles and in the end got the following:


(generator.longname =~ /-1/ ? generator.longname.gsub('-1','') : generator.longname)

cryptic isnt it?

This is how this can be replaced with sql or select statment using rails.


 Generator.find(:all,:select => "REPLACE(longname,'-1','') as longname")

More on the REPLACE function can be found here