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

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