title: Arithmetic Overflow in SQL Server
body: <p>I have a stored procedure which calculates the distance between two coordinate pairs as a float. I'm trying to use this to filter a list of values but getting an arithmetic overflow error. The query is:</p>
  
  <pre><code>SELECT * FROM Housing h  WHERE convert(float, dbo.CalculateDistance(35, -94, h.Latitude, h.Longitude)) &lt;= 30.0
  </code></pre>
  
  <p>Which errors with:</p>
  
  <blockquote>
    <p>Msg 8115, Level 16, State 6, Line 1 Arithmetic overflow error
    converting float to data type numeric.</p>
  </blockquote>
  
  <p>The stored procedure for reference:</p>
  
  <pre><code>CREATE FUNCTION [dbo].[CalculateDistance]
      (@Longitude1 DECIMAL(8,5),
      @Latitude1   DECIMAL(8,5),
      @Longitude2  DECIMAL(8,5),
      @Latitude2   DECIMAL(8,5))
  RETURNS FLOAT
  AS
  BEGIN
  DECLARE @Temp FLOAT
  
  SET @Temp = SIN(@Latitude1/57.2957795130823) * SIN(@Latitude2/57.2957795130823) + COS(@Latitude1/57.2957795130823) * COS(@Latitude2/57.2957795130823) * COS(@Longitude2/57.2957795130823 - @Longitude1/57.2957795130823)
  
  IF @Temp &gt; 1
      SET @Temp = 1
  ELSE IF @Temp &lt; -1
      SET @Temp = -1
  
  RETURN (3958.75586574 * ACOS(@Temp) )
  
  END
  </code></pre>
  
  <p>'ve also tried converting the result to decimal with no effect.</p>