The below function can be used as a alternate for GREATEST() function in MYSQL:
Create a function with below code in your SQL SERVER database:
Below function compares 11 columns in a table.
CREATE FUNCTION dbo.fnGreatest
( @Value0 sql_variant,
@Value1 sql_variant,
@Value2 sql_variant,
@Value3 sql_variant,
@Value4 sql_variant,
@Value5 sql_variant,
@Value6 sql_variant,
@Value7 sql_variant,
@Value8 sql_variant,
@Value9 sql_variant,
@Value10 sql_variant
)
RETURNS sql_variant
AS
BEGIN
DECLARE @ReturnValue sql_variant
DECLARE @MaxTable table
( RowID int IDENTITY,
MaxColumn sql_variant
)
INSERT INTO @MaxTable VALUES ( @Value0 )
INSERT INTO @MaxTable VALUES ( @Value1 )
INSERT INTO @MaxTable VALUES ( @Value2 )
INSERT INTO @MaxTable VALUES ( @Value4 )
INSERT INTO @MaxTable VALUES ( @Value5 )
INSERT INTO @MaxTable VALUES ( @Value6 )
INSERT INTO @MaxTable VALUES ( @Value7 )
INSERT INTO @MaxTable VALUES ( @Value8 )
INSERT INTO @MaxTable VALUES ( @Value9 )
INSERT INTO @MaxTable VALUES ( @Value10 )
SELECT @ReturnValue = MAX(MaxColumn)
FROM @MaxTable
RETURN @ReturnValue
END
GO
SELECT fnGreatest(date1,date2,date3.....date11) from <tablename>
No comments:
Post a Comment