Function in SQL Server 2008 similar to GREATEST in mysql?

前端 未结 5 1378
孤独总比滥情好
孤独总比滥情好 2020-11-27 22:11

I want to find the maximum value of multiple columns.

MySQL supports the GREATEST function but SQL Server doesn\'t.

Is there any function similar to this in

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 22:45

    For this, I created a scalar function as follows:

    CREATE FUNCTION [dbo].[MaxOrNull](@val1 int, @val2 int)
    returns int
    as
    begin
        if @val1 >= @val2 RETURN @val1
        if @val1 < @val2 RETURN @val2
    
        RETURN NULL
    end
    

    It's the most elegant solution and can be used anywhere in your SQL code.

提交回复
热议问题