`CASE...WHEN...THEN...END` Support
See original GitHub issueThis is a bigger one, but, any plans on implementing CASE support?
https://modern-sql.com/feature/case
Looks like it is supported by: BigQuery, Db2, MariaDB, MySQL, Oracle DB, PostgreSQL, SQL Server, SQLite, so basically all major engines.
Syntax:
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
WHEN conditionN THEN resultN
ELSE result
END;
Probably would fit well as another method on ExpressionBuilder, ie:
db.selectFrom("table")
.select(eb => {
return eb.case(cb => {
return cb.when("expression1", "expression2")
.when("expression3", "expression4")
.else("expression5")
})
})
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:8 (2 by maintainers)
Top Results From Across the Web
SQL CASE Expression - W3Schools
The CASE expression goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a...
Read more >CASE (Transact-SQL) - SQL Server - Microsoft Learn
The searched CASE expression evaluates a set of Boolean expressions to determine the result. Both formats support an optional ELSE argument.
Read more >SQL CASE | Intermediate SQL - Mode Analytics
Every CASE statement must end with the END statement. The ELSE statement is optional, and provides a way to capture values not specified...
Read more >SQL Server: CASE Statement - TechOnTheNet
In SQL Server (Transact-SQL), the CASE statement has the functionality of an IF-THEN-ELSE statement. You can use the CASE statement within a SQL...
Read more >CASE statement in SQL
Case Statement with Order by clause ; Select EmployeeName,Gender,Salary ; from Employee ; ORDER BY CASE Gender ; WHEN 'F' THEN Salary End...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So we’d have
orWhen
,orWhenExists
etc? That’s an option and it would take care of thecase
statement but it’s not reusable. I’m talking about a builder that could be used to build any expression. For exampleIf we go down the road of adding builders for all kinds of expressions, we need to really think it through and create a cohesive expression builder instead of adding different things ad-hoc. I’m also not sure we should do it at all. The code quickly becomes unreadable and it’s better just to write raw SQL and pay the small type-unsafety penalty.
Let’s think about the case builder for example. The syntax could indeed be something like this
The expressions can be anything but let’s say they are simple comparison operations. We’d have two options:
Since we are on the road of full type-safety, we’d of course want to use the expression builder. We’d get something like this:
In this example I used two non-existent expression builder methods
bin
andval
. That’s not too bad yet, but compared to the raw SQL it’s quite a bit of boilerplate