Indentation seems off here. I think you can do 

```ruby
ActiveRecord::Base.connection.execute(
  <<-SQL
    UPDATE users
    SET yammer_network_name = TRIM(BOTH ' ' FROM SUBSTRING(extra FROM '\"network_name\":\"(.+?)\"'))
    WHERE substring(extra FROM '\"network_name\":\"(.+?)\"') IS NOT NULL
  SQL
)
```