Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. LesserOf function

LesserOf function

Scheduled Pinned Locked Moved Database
databasesql-serversysadminquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Online
    P Online
    PIEBALDconsult
    wrote on last edited by
    #1

    Today I wanted a function (in SQL Server 2008) to return the lesser of two integers, but it didn't seem to exist, so I wrote one, then decided that it might be somewhat more useful sometime in the future if it wasn't coded for integers, so I changed it to use numeric instead:

    CREATE FUNCTION dbo.LesserOf
    (
    @Op1 NUMERIC
    ,
    @Op2 NUMERIC
    )
    RETURNS NUMERIC
    AS
    BEGIN
    DECLARE @result NUMERIC

    IF ( @Op1 < @Op2 )
    SET @result = @Op1
    ELSE
    SET @result = @Op2

    RETURN @result
    END

    Is there some odd limitation involved with numeric that I don't know about (I never use them)? Is there a better way to implement this simple function (not a CASE)? Is there a built-in LesserOf function?

    A J 2 Replies Last reply
    0
    • P PIEBALDconsult

      Today I wanted a function (in SQL Server 2008) to return the lesser of two integers, but it didn't seem to exist, so I wrote one, then decided that it might be somewhat more useful sometime in the future if it wasn't coded for integers, so I changed it to use numeric instead:

      CREATE FUNCTION dbo.LesserOf
      (
      @Op1 NUMERIC
      ,
      @Op2 NUMERIC
      )
      RETURNS NUMERIC
      AS
      BEGIN
      DECLARE @result NUMERIC

      IF ( @Op1 < @Op2 )
      SET @result = @Op1
      ELSE
      SET @result = @Op2

      RETURN @result
      END

      Is there some odd limitation involved with numeric that I don't know about (I never use them)? Is there a better way to implement this simple function (not a CASE)? Is there a built-in LesserOf function?

      A Offline
      A Offline
      Andy_L_J
      wrote on last edited by
      #2

      Try this as an exercise....

      DECLARE @N NUMERIC -- No precision or dcale
      SET @N = 12.345

      PRINT @N

      DECLARE @N2 NUMERIC(5,3)
      SET @N2 = 12.345

      PRINT @N2

      DECLARE @N3 NUMERIC(4,3)
      SET @N3 = 12.345

      PRINT @N3

      The precision and scale parameters are important!

      I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

      P 1 Reply Last reply
      0
      • P PIEBALDconsult

        Today I wanted a function (in SQL Server 2008) to return the lesser of two integers, but it didn't seem to exist, so I wrote one, then decided that it might be somewhat more useful sometime in the future if it wasn't coded for integers, so I changed it to use numeric instead:

        CREATE FUNCTION dbo.LesserOf
        (
        @Op1 NUMERIC
        ,
        @Op2 NUMERIC
        )
        RETURNS NUMERIC
        AS
        BEGIN
        DECLARE @result NUMERIC

        IF ( @Op1 < @Op2 )
        SET @result = @Op1
        ELSE
        SET @result = @Op2

        RETURN @result
        END

        Is there some odd limitation involved with numeric that I don't know about (I never use them)? Is there a better way to implement this simple function (not a CASE)? Is there a built-in LesserOf function?

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        I think T-SQL needs the Least function from PL-SQL. It's more generic.

        Light moves faster than sound. That is why some people appear bright, until you hear them speak. List of common misconceptions

        1 Reply Last reply
        0
        • A Andy_L_J

          Try this as an exercise....

          DECLARE @N NUMERIC -- No precision or dcale
          SET @N = 12.345

          PRINT @N

          DECLARE @N2 NUMERIC(5,3)
          SET @N2 = 12.345

          PRINT @N2

          DECLARE @N3 NUMERIC(4,3)
          SET @N3 = 12.345

          PRINT @N3

          The precision and scale parameters are important!

          I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Ah, I thought that might be the case, thanks. So what do you suggest? real?

          A 1 Reply Last reply
          0
          • P PIEBALDconsult

            Ah, I thought that might be the case, thanks. So what do you suggest? real?

            A Offline
            A Offline
            Andy_L_J
            wrote on last edited by
            #5

            You could do this...

            CREATE FUNCTION dbo.LesserOf
            (
            @Op1 NUMERIC(18,7)
            ,
            @Op2 NUMERIC(18,7)
            )
            RETURNS NUMERIC(18,7)
            AS
            BEGIN
            DECLARE @result NUMERIC(18,7)

            IF ( @Op1 < @Op2 )
            SET @result = @Op1
            ELSE
            SET @result = @Op2

            RETURN @result
            END

            ...or set the precision and scale to something bigger (20,7)...will give 13 significant numbers and 7 decimal places. (I don't usually use real.)

            I don't speak Idiot - please talk slowly and clearly 'This space for rent' Driven to the arms of Heineken by the wife

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups