Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting maximum value of float in SQL programmatically

Tags:

Is there an method for programmatically (in T-SQL) retrieving the maximum (and minimum) value of a datatype? That it would act like float.MaxValue in C#.

I would like to use it in some selection when the parameter does not equal any actual values in the database, so I would use something like

declare @min float
declare @max float
--fill @min and @max, can be null if undefined
select * from foo 
  where bar between isnull(@min,0 ) and isnull(@max,max(float)/*magic*/)
like image 480
Axarydax Avatar asked Apr 23 '10 15:04

Axarydax


People also ask

How do you find maximum value in SQL?

To find the max value of a column, use the MAX() aggregate function; it takes as its argument the name of the column for which you want to find the maximum value. If you have not specified any other columns in the SELECT clause, the maximum will be calculated for all records in the table.

What is float MaxValue?

Represents the largest possible value of Single. This field is constant. public: float MaxValue = 3.40282347E+38; C# Copy.

How do you find the maximum value of a decimal?

So for decimal(5,2) it would be 999.99 . 5 is the total number of decimals to the left and to the right of the decimal point. 2 is the number of decimals to the right of the decimal point. The maximum possible range for decimals is -10^38 + 1 through 10^38 - 1.


2 Answers

Though there doesn't appear to be any inline way to get the min or max values, there's a solution somebody put together:

 CREATE TABLE datatype_extrema 
  (min_bit bit NOT NULL DEFAULT (0) CHECK (min_Bit=0) 
  ,max_bit           AS CAST(0x1 AS bit) 
  ,min_tinyint       AS CAST(0x00 AS tinyint) 
  ,max_tinyint       AS CAST(0xFF AS tinyint) 
  ,min_smallint      AS CAST(0x8000 AS smallint) 
  ,max_smallint      AS CAST(0x7FFF AS smallint) 
  ,min_int           AS CAST(0x80000000 AS int) 
  ,max_int           AS CAST(0x7FFFFFFF AS int) 
  ,min_bigint        AS CAST(0x8000000000000000 AS bigint) 
  ,max_bigint        AS CAST(0x7FFFFFFFFFFFFFFF AS bigint)
  ,min_float         AS CAST('-1.79E+308' AS float)
  ,max_float         AS CAST('1.79E+308' AS float)
  ,min_real          AS CAST('-3.40E+38' AS real)
  ,max_real          AS CAST('3.40E+38' AS real)
  ,min_smalldatetime AS CAST('19000101 00:00' AS smalldatetime) 
  ,max_smalldatetime AS CAST('20790606 23:59' AS smalldatetime) 
  ,min_datetime      AS CAST('17530101 00:00:00.000' AS datetime) 
  ,max_datetime      AS CAST('99991231 23:59:59.997' AS datetime) 
  )
  INSERT INTO datatype_extrema DEFAULT VALUES 
  GO 
  CREATE TRIGGER nochange_datatype_extrema 
  ON datatype_extrema INSTEAD OF INSERT, UPDATE, DELETE 
  AS BEGIN 
    RAISERROR ('No changes allowed for table datatype_extrema.', 16, 1) 
    ROLLBACK TRANSACTION 
  END 
  GO 

After that, you can either copy a maximum value to a local variable or (when using queries) cross join with this table.

  Declare @max_int int 
  Set @max_int=(SELECT max_int FROM datatype_extrema) 
  IF COALESCE(@FirstInt, @max_int) < COALESCE(@SecondInt, 0) 
like image 198
SqlRyan Avatar answered Sep 19 '22 14:09

SqlRyan


Here are the defaults for the float and real type (that's missing in the accepted answer):

select
    CAST('-1.79E+308' AS float) as MinFloat,
    CAST('1.79E+308' AS float) as MaxFloat,
    CAST('-3.40E+38' AS real) as MinReal,
    CAST('3.40E+38' AS real) as MaxReal

Unfortunately it is not possible to convert them from a varbinary, but varchar works without any problems.

like image 23
Oliver Avatar answered Sep 20 '22 14:09

Oliver