1

I'm attempting to get a few particular columns from a table of people with a catch: I would like to concatenate the first two columns together.

More specifically, I have the following array:

@people=@company.people.select(fname, lname, email, telephone, street, city, state, zip,)

I would like, however to concatenate fname and lname together into one column (call it name) so they appear like this:

{lname}, {fname}

I suppose I could do this using a loop with an index but is there a more elegant solution?

Thanks in advance

neanderslob
  • 2,633
  • 6
  • 40
  • 82

2 Answers2

2

You can try

@company.people.select('({fname}, || ' ' || {lname} as name), email, telephone, street, city, state, zip')
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
1

You can use the database to help you with this. For example, if you're using Postgres, you can do something like

@people = @company.people.select("fname || ', ' || lname as name")

If you're using Mysql, the answers to MySQL select with CONCAT condition can help you with the correct syntax.

Community
  • 1
  • 1
eugen
  • 8,916
  • 11
  • 57
  • 65