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