Wednesday, June 8, 2022

GREATEST and LEAST Functions in SQL SERVER

 


     GREATEST and LEAST functions are available in Azure SQL and SQL Server 2022. GREATEST returns the maximum value from a list of expressions. LEAST returns the minimum value from a list of expressions. 

     The Highest number of expressions you can pass to these functions is 254. All expressions must be comparable for these functions to work. For example, SQL Server can automatically convert the following string to an integer and find the greatest number.


SQL Server can not convert the following data types then It throws an error.

NULL values will be ignored if you have any NULL values in the expression list.

The following example is a way to use these functions in a table. I look for the records that have UnitPrice is higher than 200 OR TaxAmount is higher than 200.




You can even use these functions with ORDER BY. The following query execution plan is the same if you ORDER BY TotalDryItems.





No comments:

Post a Comment