Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 643027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:14:18+00:00 2026-05-13T21:14:18+00:00

I created the following function to simplify a piece of particularly complex code. CREATE

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:14:18+00:00Added an answer on May 13, 2026 at 9:14 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 369k
  • Answers 369k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Check whether you can connect to your SVN repo from… May 14, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer It's actually working: string blub = "C://test/test/test.png"; string blub2 =… May 14, 2026 at 6:21 pm
  • Editorial Team
    Editorial Team added an answer Got it. I am using IWindsorInstaller to implement packages: public… May 14, 2026 at 6:21 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.