User Defined Function (UDF) to Convert Weight between Kilograms and Pounds in SQL Server

To create a User Defined Function (UDF) that converts weight from Kilograms to Pounds, that uses decimal(7,3) data type with precision 7 and scale 3 for input and output weight values, use the following command:

CREATE FUNCTION [dbo].[ufn_kg2lb] (@kg decimal(7,3))
RETURNS decimal(7,3)
AS
BEGIN
RETURN (@kg * 2.20462)
END

To use this user-defined function (for example to convert 34.148 kg to lb):

SELECT dbo.ufn_kg2lb (34.148)

ufn_kg2lb

To create a User Defined Function (UDF) that converts weight from Pounds to Kilograms, that uses decimal(7,3) data type with precision 7 and scale 3 for input and output weight values, use the following command:

CREATE FUNCTION [dbo].[ufn_lb2kg] (@lb decimal(7,3))
RETURNS decimal(7,3)
AS
BEGIN
RETURN (@lb * 0.453592)
END

To use this user-defined function (for example to convert 142.561 lb to kg):

SELECT dbo.ufn_lb2kg (142.561)

ufn_lb2kg

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