Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

169
Views
Elixir / Ecto / Postgres Select multiple columns as one

I just want do execute an Ecto query with an simple concatination of two or more columns.

I think the following elixir pseudo code already shows what I try to do:

customers = Customer.undeleted(
  from c in Customer,
  select: %{id: c.id, name: c.name <> " – " <> c.street},
  order_by: c.name
) |> Repo.all

It drives me crazy cuz in SQL it is easy like this: ...SELECT c.id, concat(c.name, ' - ', c,street) AS name

Any ideas how to solve this with ecto querys ?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You cannot use <> in a select expression in Ecto. If you want to call concat like that, you can use fragment:

select: %{id: c.id, name: fragment("concat(?, ' - ', ?)", c.name, c.street)},
over 4 years ago · Santiago Trujillo Report

0

To add to @Dogbert answer, you can clean up the code a bit by putting the SQL function fragment in a custom macro as described in the docs:

defmodule CustomSQLFunctions do
  defmacro concat(left, mid, right) do
    quote do
      fragment("concat(?, ?, ?)", unquote(left), unquote(mid), unquote(right))
    end
  end
end

Then import for use in query

import CustomSQLFunctions

customers = Customer.undeleted(
  from c in Customer,
  select: %{id: c.id, name: concat(c.name, ' - ', c.street)},
  order_by: c.name
) |> Repo.all
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!