Home > Software design >  Rails query to substitute field and query
Rails query to substitute field and query

Time:01-17

could someone please help me to write a query to substitute hyphens in rails DB field with space? For eg: If I have a field called 'name' in a table User having a value 'asdc-sd bc', and want to remove special characters like '-' and replace it with space to match with a given name 'asdc sd bc'.

I tried using lower to convert the name to lowercase but am not able to find out how to substitute the hyphens with space. Please help!

CodePudding user response:

You can use SQL REPLACE function to replace - with spaces(' '). For example, if you are query on name column of User model, you can write you query like below:

query_str = 'asdc sd bc'
User.where("REPLACE(name, '-', ' ') = ?", query_str)
  • Related