here is a basic one you can work with, I'd be careful using this in queries, as it will slow them down in proportion to the number of rows it is used on:
CREATE FUNCTION [dbo].[DSGetMinimum] (@First sql_variant, @Second sql_variant)
RETURNS varchar(8000)
AS
BEGIN
DECLARE @Value varchar(8000)
IF SQL_VARIANT_PROPERTY(@First,'BaseType')=SQL_VARIANT_PROPERTY(@Second,'BaseType')
OR @First IS NULL OR @Second IS NULL
BEGIN
IF SQL_VARIANT_PROPERTY(@First,'BaseType')='datetime'
BEGIN
IF CONVERT(datetime,@First)<CONVERT(datetime,@Second)
BEGIN
SET @Value=CONVERT(char(23),@First,121)
END
ELSE
BEGIN
SET @Value=CONVERT(char(23),@Second,121)
END
END --IF datetime
ELSE
BEGIN
IF @First < @Second
SET @Value=CONVERT(varchar(8000),@First)
ELSE
SET @Value=CONVERT(varchar(8000),@Second)
END
END --IF types the same
RETURN @Value
END
GO
EDIT
Test Code:
DECLARE @D1 datetime , @D2 datetime
DECLARE @I1 int , @I2 int
DECLARE @V1 varchar(5) , @V2 varchar(5)
SELECT @D1='1/1/2010', @D2='1/2/2010'
,@I1=5 , @I2=999
,@V1='abc' , @V2='xyz'
PRINT dbo.DSGetMinimumInt(@D1,@D2)
PRINT dbo.DSGetMinimumInt(@I1,@I2)
PRINT dbo.DSGetMinimumInt(@V1,@V2)
Test Output:
2010-01-01 00:00:00.000
5
abc
If you are going to use this in a query, I would just use an inline CASE statement, which would be MUCH faster then the UDF:
CASE
WHEN @valueAnyType1<@ValueAnyType2 THEN @valueAnyType1
ELSE @ValueAnyType2
END
you can add protections for NULL if necessary:
CASE
WHEN @valueAnyType1<=ISNULL(@ValueAnyType2,@valueAnyType1) THEN @valueAnyType1
ELSE @ValueAnyType2
END