User Defined Function (UDF) to Convert Length between Meters and Feet in SQL Server

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

CREATE FUNCTION [dbo].[ufn_m2ft] (@m decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@m * 3.28084)
END

To use this user-defined function (for example to convert 16.423 meters to feet):

SELECT dbo.ufn_m2ft (16.423)

ufn_m2ft

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

CREATE FUNCTION [dbo].[ufn_ft2m] (@ft decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@ft * 0.3048)
END

To use this user-defined function (for example to convert 431.239 feet to meters):

SELECT dbo.ufn_ft2m (431.239)

ufn_ft2m

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="">