User Defined Function (UDF) to Convert Length between Centimeters and Inches in SQL Server

To create a User Defined Function (UDF) that converts length from Centimeters to Inches, 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_cm2in] (@cm decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@cm * 0.393701)
END

To use this user-defined function (for example to convert 11.419 centimeters to inches):

SELECT dbo.ufn_cm2in (11.419)

ufn_cm2in

To create a User Defined Function (UDF) that converts length from Inches to Centimeters, 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_in2cm] (@in decimal(10,3))
RETURNS decimal(10,3)
AS
BEGIN
RETURN (@in * 2.54)
END

To use this user-defined function (for example to convert 4.237 inches to centimeters):

SELECT dbo.ufn_in2cm (4.237)

ufn_in2cm

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