---
title: "Is there a Max function in SQL Server that takes two values like Math.Max in .NET?"  
description: "Is there a Max function in SQL Server that takes two values like Math.Max in .NET?"  
author: "Revati S Misra"  
published: 2023-07-11  
updated: 2023-07-12  
canonical: https://www.mindstick.com/forum/159039/is-there-a-max-function-in-sql-server-that-takes-two-values-like-math-max-in-dot-net  
category: "mssql server"  
tags: ["mssql server", "sql server", "sql"]  
reading_time: 2 minutes  

---

# Is there a Max function in SQL Server that takes two values like Math.Max in .NET?

Is there a [Max](https://www.mindstick.com/articles/1155/count-max-min-function-in-excel) [function in SQL](https://www.mindstick.com/forum/156839/what-is-function-in-sql-server) [Server](https://www.mindstick.com/articles/43769/what-is-serverless-architecture-is-it-worth-switching-over) that takes two [values](https://www.mindstick.com/forum/327/sum-textbox-values) like Math.Max in .NET?

## Replies

### Reply by Aryan Kumar

No, there is no built-in MAX [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) in [SQL Server](https://www.mindstick.com/articles/12999/what-is-table-valued-function-in-sql-server) that takes two values. The MAX function only takes one parameter, and it returns the maximum value of all the values in a column.

However, there are a few ways to get the maximum of two values in SQL Server. One way is to use the following code:

SQL

```plaintext
SELECT MAX(value1, value2);
```

This code will return the maximum value of the two values `value1` and `value2`.

Another way to get the maximum of two values in SQL Server is to use a CASE statement. The following code shows how to use a CASE statement to get the maximum of two values:

SQL

```plaintext
SELECT
CASE
WHEN value1 > value2 THEN value1
ELSE value2
END AS max_value;
```

This code will return the maximum value of the two values `value1` and `value2`. If `value1` is greater than `value2`, then the `max_value` column will contain the value of `value1`. Otherwise, the `max_value` column will contain the value of `value2`.

Finally, you can also use a user-defined function (UDF) to get the maximum of two values in SQL Server. The following code shows how to create a UDF to get the maximum of two values:

SQL

```plaintext
CREATE FUNCTION max_of_two(@value1 INT, @value2 INT)
RETURNS INT
AS
BEGIN
DECLARE @max_value INT;
SET @max_value = @value1;
IF @value2 > @value1
SET @max_value = @value2;
RETURN @max_value;
END;
```

This code creates a UDF called `max_of_two` that takes two integer parameters and returns the maximum value of the two parameters.


---

Original Source: https://www.mindstick.com/forum/159039/is-there-a-max-function-in-sql-server-that-takes-two-values-like-math-max-in-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
