Least Function

Least() Function

Last updated:

Warning: Review and test in a non-production environment before running.

Back to results

-- Examples from Microsoft Learn: LEAST (Transact-SQL) – https://learn.microsoft.com/en-us/sql/t-sql/functions/logical-functions-least-transact-sql

Least Function

SQL Server 2022 (16.x) Preview

 

This function returns the minimum value from a list of one or more expressions.
LEAST ( expression1 [ ,...expressionN ] )

examples:

SELECT LEAST ( '6.62', 3.1415, N'7' ) AS LeastVal; 
GO

LeastVal 
------- 
 3.1415 

(1 rows affected)

 

SELECT LEAST ('Glacier', N'Joshua Tree', 'Mount Rainier') AS LeastString;  
GO

LeastString 
------------- 
Glacier 

(1 rows affected)

 

SELECT P.Name, P.SellStartDate, P.DiscontinuedDate, PM.ModifiedDate AS ModelModifiedDate
    , LEAST(P.SellStartDate, P.DiscontinuedDate, PM.ModifiedDate) AS EarliestDate
FROM SalesLT.Product AS P
INNER JOIN SalesLT.ProductModel AS PM on P.ProductModelID = PM.ProductModelID
WHERE LEAST(P.SellStartDate, P.DiscontinuedDate, PM.ModifiedDate) >='2007-01-01'
AND P.SellStartDate >='2007-01-01'
AND P.Name LIKE 'Touring %'
ORDER BY P.Name;

Name                   SellStartDate           DiscontinuedDate    ModelModifiedDate       EarliestDate
---------------------- ----------------------- ------------------- ----------------------- -----------------------
Touring Pedal          2007-07-01 00:00:00.000 NULL                2009-05-16 16:34:29.027 2007-07-01 00:00:00.000
Touring Tire           2007-07-01 00:00:00.000 NULL                2007-06-01 00:00:00.000 2007-06-01 00:00:00.000
Touring Tire Tube      2007-07-01 00:00:00.000 NULL                2007-06-01 00:00:00.000 2007-06-01 00:00:00.000

(3 rows affected)