Logo

SQL Server GREATEROF Function

Updated 2023-10-20 15:51:12.513000

Description

Use the scalar function GREATEROF to return the greater of two values passed into the function.

Syntax

SELECT [wctMath].[wct].[GREATEROF](
  <@Val1, sql_variant,>
 ,<@Val2, sql_variant,>))

Arguments

@Val1

The first value to be evaluated by the function.

@Val2

The second value to be evaluated by the function.

Return Type

sql_variant

Remarks

If the data types of the expressions are different, @Val2 will be converted to whatever data type @Val1 is for the purpose of the comparison. The datatype of the value returned will remain as it was originally passed to the function.

If the comparison is based on a character comparison, one character is considered greater than another if it has a higher character set value.

If the comparison is between a number and a date, the date is converted to a float equal to the number of days from 1-Jan-1900 having a fractional component equal to the time component's percentage of a day.

Use the LESSEROF function to return the lesser of two values.

If @Val1 is NULL or @Val2 is NULL then the other value is returned by the function.

Examples

SELECT wct.GREATEROF(9, 143) as GREATEROF;

This produces the following result.

GREATEROF
143
SELECT wct.GREATEROF('9', '143') as GREATEROF;

This produces the following result.

GREATEROF
9
SELECT wct.GREATEROF(CAST('2013-03-27' as datetime), 41358.5) as GREATEROF;

This produces the following result.

GREATEROF
41358.5
SELECT wct.GREATEROF(CAST('2013-03-27' as datetime), cast(41358.5 as datetime)) 
          as GREATEROF;

This produces the following result.

GREATEROF
2013-03-27 12:00:00.000
SELECT wct.GREATEROF(wct.GREATEROF(wct.GREATEROF(9, 143), 162), 799) as GREATEROF;

This produces the following result.

GREATEROF
799
SELECT wct.GREATEROF(103216549870316540031,905168703210654601321) as GREATEROF;

This produces the following result.

GREATEROF
905168703210654601321