Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda