SQL Server: Translate Math.Max and Math.Min
See original GitHub issueToday Math.Max / Math.Min is not being translated for SQL Server - only for sqlite as introduced in #10533
Transact-SQL does not have an MIN or MAX function, so there’s no 1-to-1 mapping, but it is however possible to translate it into a CASE WHEN
statement.
For example
Math.Max(Column1, Column2)
is equivalent to
CASE
WHEN Column1 >= Column2 THEN Column1
ELSE Column2
END
As a suggestion, I made a reference implementation by adding the following translation to SqlServerMathTranslator.cs
For Math.Max it looks like this:
var left = arguments[0];
var right = arguments[1];
var typeMapping = ExpressionExtensions.InferTypeMapping(left, right);
left = _sqlExpressionFactory.ApplyTypeMapping(left, typeMapping);
right = _sqlExpressionFactory.ApplyTypeMapping(right, typeMapping);
return _sqlExpressionFactory.Case(
new[] { new CaseWhenClause(_sqlExpressionFactory.GreaterThanOrEqual(left, right), left) },
right);
and Math.Min:
// same type mapping...
return _sqlExpressionFactory.Case(
new[] { new CaseWhenClause(_sqlExpressionFactory.LessThanOrEqual(left, right), left) },
right);
Issue Analytics
- State:
- Created a year ago
- Reactions:2
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Is there a Max function in SQL Server that takes two values ...
It's easy to swap out the "MAX" with "MIN", "AVG", or "SUM". You can use any aggregate function to find the aggregate over...
Read more >Working with the SQL MIN function in SQL Server
The SQL MIN function is an aggregate function that is used to find the minimum values in columns or rows in a table....
Read more >Mathematical Functions (Transact-SQL) - SQL Server
Arithmetic functions, such as ABS, CEILING, DEGREES, FLOOR, POWER, RADIANS, and SIGN, return a value having the same data type as the input ......
Read more >SQL Server MAX(): Get Maximum Value in a Column
The MAX() function is an aggregate function that returns the maximum value in the column or expression.
Read more >SQL MIN() and MAX() Functions
The MAX() function returns the largest value of the selected column. MIN() Syntax. SELECT MIN(column_name) FROM table_name. WHERE condition;. MAX() Syntax.
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 Free
Top 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
SQL Server is introducing the GREATEST and LEAST functions, which are standard across relational databases (see announcements). These are already available on Azure SQL, and will apparently be available in the next version of on-premise SQL Server.
So while they aren’t generally available (except on Azure), I’m not sure it makes sense to introduce the CASE/WHEN translation proposed above (we’ll probably want to add GREATEST/LEAST translations when they’re available). Note that it should be easy to do the above mapping with user-defined function mapping.
@MKatGH you’re correct, it would just be for convenience purposes. string.IsNullOrEmpty for example is also trivial to express out of the box, but there is a translator for that. I don’t really have a strong opinion for or against it, it’s just a mere suggestion