User Defined Function (UDF) to Convert Area between Hectares and Acres in SQL Server

To create a User Defined Function (UDF) that converts area from Hectares to Acres, that uses decimal(10,4) data type with precision 10 and scale 4 for input and output area values, use the following command:

CREATE FUNCTION [dbo].[ufn_ha2ac] (@ha decimal(10,4))
RETURNS decimal(10,4)
AS
BEGIN
RETURN (@ha * 2.47105381)
END

To use this user-defined function (for example to convert 21.1824 hectares to acres):

SELECT dbo.ufn_ha2ac (21.1824)

ufn_ha2ac

To create a User Defined Function (UDF) that converts area from Acres to Hectares, that uses decimal(10,4) data type with precision 10 and scale 4 for input and output area values, use the following command:

CREATE FUNCTION [dbo].[ufn_ac2ha] (@ac decimal(10,4))
RETURNS decimal(10,4)
AS
BEGIN
RETURN (@ac * 0.404685642)
END

To use this user-defined function (for example to convert 95.1411 acres to hectares):

SELECT dbo.ufn_ac2ha (95.1411)

ufn_ac2ha

If the post helped you, please share it:
Pin It

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">