
RudManusachi
SQL CASE with Ecto
Hi there!
Recently I was playing around with extracting and updating data in the DB and for fun challenged myself to try to implement a nice-looking function/macro to do SQL CASE
with Ecto.
Wanted to share with you the results and thought it might be a good point to start blogging.
Any feedback is appreciated =)
Spoiler alert, throughout the blog post we are putting up together a sql_case/2
macro that could be used as:
from(m in "movies",
select:
{m.title,
sql_case(m.rating, [
[when: "G", then: "General Audiences"],
[when: "R", then: "Restricted"],
[when: "PG", then: "Parental Guidance Suggested"],
[when: "NC-17", then: "Clearly Adult"],
[else: m.rating]
])}
)
Most Liked

AstonJ
Looks good Rudolf!
Just a quick note that when you start creating a thread here, if you paste the link into the title field
first this will add the link as a ‘featured link’ to your thread - these are the links show on our homepage and go some way towards your SEO as we don’t add a rel-nofollow to user-submitted blog posts I have added it manually for you for this post
Popular Backend topics










Other popular topics










Latest in Backend
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /textmate
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /c
- /julia
- /neovim