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