SQL Server 2022 and the GREATEST / LEAST T-SQL commands

Hi Guys,


Welcome back to this little new midweek post.
Today we will see two more simple but useful new T-SQL commmands added in SQL Server 2022.
 
I usually don't like commands that aren't ANSI standard as much, but now these commands are used by more than one database engine. For example, our new GREATEST and LEAST commands are supported by PostgreSQL, MySQL and Oracle.

So if they are comfortable, they are welcome!

Enjoy the reading!


GREATEST / LEAST commands 


These new two functions returns the maximum value and then mininum value from a list of one or more expressions.
 
As paramters they accept a list of comma-separated expressions of any comparable data type. Furthermore these functions requires at least one argument and supports no more than 254 arguments. 
 
For example we can write:

SELECT GREATEST('Brad Pitt', N'Angelina Jolie', 'Tom Hanks') AS VALUE;  
       
or

SELECT LEAST('Brad Pitt', N'Angelina Jolie', 'Tom Hanks') AS VALUE ;  

 
In the first case we will get:


VALUE 
------------- 
Tom Hanks 

(1 rows affected)  

       
else

VALUE 
------------- 
Angelina Jolie

(1 rows affected)  


Obviousbly these functions become comfortable when we have more than two parameters.


That's all mate!
 
Stay tuned for the next post and follow me here and on linkedin!

 












Comments

I Post più popolari

SQL Server, execution plan and the lazy spool (clearly explained)

SQL Server, datetime vs. datetime2

La clausola NOLOCK. Approfondiamo e facciamo chiarezza!