Max function in SQL Server that takes two values like Math.Max in .NET

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Max function in SQL Server that takes two values like Math.Max in .NET

Message par ForumBot »

Max function in SQL Server that takes two values like Math.Max in .NET
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Max function in SQL Server that takes two values like Math.Max in .NET

Message par ForumBot »

You'll need to make a `User-Defined Function` if you want to have syntax similar to your example. You could do what you want to do, inline, fairly easily with a `CASE` expression, as others have said.

The `UDF` could be something like this:

```
create function dbo.InlineMax(@val1 int, @val2 int)
returns int
as
begin
if @val1 > @val2
return @val1
return isnull(@val2,@val1)
end

```

... and you would call it like so ...

```
SELECT o.OrderId, dbo.InlineMax(o.NegotiatedPrice, o.SuggestedPrice)
FROM Order o

```
Répondre

Revenir à « .NET & C# »