0

I am using ruby on rails with postgresql. I have a table called "teams" with column "team_name". This "team_name" contains values like "1(Team1)", "1(Team2)", "2(Team1)", etc. Now want to take Team1, Team2, Team3 from those team names using sql query. If I use split_part as follows

   sql = "SELECT SPLIT_PART(team_name,'(',2) FROM teams where id=20"
   result = ActiveRecord::Base.connection.execute(sql).to_a

In the result I am getting

    [{"split_part"=>"Team1)"}] 

but I want the following result

    [{"split_part"=>"Team1"}] 
1

1 Answer 1

1

You can use SUBSTRING, but special characters must be escaped (backslash in this case):

Team
  .select("SUBSTRING(team_name FROM '(Team\\d+)') AS team_name")
  .where(id: id)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you but if I use pluck its giving original value instead of substring, anyway I made according changes and its working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.