Function in SQL Server 2008 similar to GREATEST in mysql?

前端 未结 5 1346
孤独总比滥情好
孤独总比滥情好 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.

    0 讨论(0)
  • 2020-11-27 22:53

    I would recommend the following solution:

    SELECT (CASE WHEN t.createdt < t.changedt THEN t.changedt ELSE t.created END) AS ChgDate
      FROM table t
    
    0 讨论(0)
  • 2020-11-27 22:53

    A possible solution:

    Create FUNCTION [dbo].[MaxOf]
        (
          @val1 INT =0,
          @val2 INT=0 ,
          @val3 INT =0,
          @val4 INT =0,
          @val5 INT =0,
          @val6 INT =0,
          @val7 INT =0,
          @val8 INT =0,
          @val9 INT =0,
          @val10 INT =0,
          @val11 INT =0,
          @val12 INT =0,
          @val13 INT =0,
          @val14 INT =0,
          @val15 INT =0,
          @val16 INT =0,
          @val17 INT =0,
          @val18 INT =0,
          @val19 INT =0,
          @val20 INT  =0)
          --OUTPUT 
         RETURNS INT  WITH SCHEMABINDING
    AS  
       BEGIN
            DECLARE  @MAX AS INT ;
            SET @MAX=0
            IF isnull(@val1,0)> isnull(@MAX,0) SET @MAX=isnull(@val1,0) 
            IF isnull(@val2,0)> isnull(@MAX,0) SET @MAX=isnull(@val2,0) 
            IF isnull(@val3,0)> isnull(@MAX,0) SET @MAX=isnull(@val3,0) 
            IF isnull(@val4,0)> isnull(@MAX,0) SET @MAX=isnull(@val4,0) 
            IF isnull(@val5,0)> isnull(@MAX,0) SET @MAX=isnull(@val5,0) 
            IF isnull(@val6,0)> isnull(@MAX,0) SET @MAX=isnull(@val6,0) 
            IF isnull(@val7,0)> isnull(@MAX,0) SET @MAX=isnull(@val7,0) 
            IF isnull(@val8,0)> isnull(@MAX,0) SET @MAX=isnull(@val8,0) 
            IF isnull(@val9,0)> isnull(@MAX,0) SET @MAX=isnull(@val9,0) 
            IF isnull(@val10,0)> isnull(@MAX,0) SET @MAX=isnull(@val10,0) 
            IF isnull(@val11,0)> isnull(@MAX,0) SET @MAX=isnull(@val11,0) 
            IF isnull(@val12,0)> isnull(@MAX,0) SET @MAX=isnull(@val12,0) 
            IF isnull(@val13,0)> isnull(@MAX,0) SET @MAX=isnull(@val13,0) 
            IF isnull(@val14,0)> isnull(@MAX,0) SET @MAX=isnull(@val14,0) 
            IF isnull(@val15,0)> isnull(@MAX,0) SET @MAX=isnull(@val15,0) 
            IF isnull(@val16,0)> isnull(@MAX,0) SET @MAX=isnull(@val16,0) 
            IF isnull(@val17,0)> isnull(@MAX,0) SET @MAX=isnull(@val17,0) 
            IF isnull(@val18,0)> isnull(@MAX,0) SET @MAX=isnull(@val18,0) 
            IF isnull(@val19,0)> isnull(@MAX,0) SET @MAX=isnull(@val19,0) 
            IF isnull(@val20,0)> isnull(@MAX,0) SET @MAX=isnull(@val20,0) 
    
            RETURN @MAX ;
        END
    

    and the call would be

    SELECT dbo.MaxOf (2,3,4,0,0,0,0,200,8,0,0,0,0,0,0,0,0,0,0,0)
    
    0 讨论(0)
  • 2020-11-27 23:02

    Try using TOP instead or MAX.

    0 讨论(0)
  • 2020-11-27 23:03

    No. But a sub query can access the columns from the outer query so you can add a sub query UNION ALL ing the columns of interest as a derived table then select the max from that.

    SELECT *, 
          (SELECT MAX(c) FROM 
                        (SELECT number AS c 
                         UNION ALL 
                         SELECT status) T) AS GreatestNumberOrStatus
    FROM master..spt_values
    

    Or a slightly more concise version as you are on 2008.

    SELECT *, 
          (SELECT MAX(c) FROM (VALUES(number),(status)) T (c)) AS Greatest
    FROM master..spt_values
    
    0 讨论(0)
提交回复
热议问题