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

To create a User Defined Function (UDF) that converts length from Meters to Yards, 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_m2yd] (@m decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@m * 1.09361)
END

To use this user-defined function (for example to convert 169.824 meters to yards):

SELECT dbo.ufn_m2yd (169.824)

ufn_m2yd

To create a User Defined Function (UDF) that converts length from Yards 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_yd2m] (@yd decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@yd * 0.9144)
END

To use this user-defined function (for example to convert 219.295 yards to meters):

SELECT dbo.ufn_yd2m (219.295)

ufn_yd2m