I created the following function to simplify a piece of particularly complex code.
CREATE FUNCTION [dbo].[DSGetMinimumInt] (@First INT, @Second INT)
RETURNS INT
AS
BEGIN
IF @First < @Second
RETURN @First
RETURN @Second
END
However, it only works for the INT datatype. I know I could create one for numeric and possibly for Varchar and Datetime.
Is it possible to create one master “Minimum” function to deal with them all? Has anyone done this?
I’ve Googled it, but come up empty.
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:
EDIT
Test Code:
Test Output:
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:
you can add protections for NULL if necessary: