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






