Technical Article

Height Format Function

,

This function takes a decimal representation of a height value and returns a properly formatted string value.
                   
For example, a height of 5 feet 9 inches, which would be passed in as the decimal 5.90, would be returned as 5'9".
                   
Similarly, a height of 4 feet 11 inches, which would be passed in as the decimal 4.11, would be returned as 4'11".

/****************************************************************************************

FileName:
format_height.sql

Description:
Contains SQL used to create the "Format_Height" function.

This function takes a decimal representation of a height
value and returns a properly formatted string value. 

For example, a height of 5 feet 9 inches, which would be 
passed in as the decimal 5.90, would be returned as 5'9". 

Similarly, a height of 4 feet 11 inches, which would be
passed in as the decimal 4.11, would be returned as 4'11".

History:
06/17/2003 -- Tim Dietrich (tim@timdietrich.us)
Initial implementation.

Notes:
None.

****************************************************************************************/

CREATE FUNCTION dbo.Format_Height (@Height DECIMAL (6,2))
RETURNS VARCHAR (6)
AS
BEGIN

DECLARE 
@FeetINTEGER,
@InchesDECIMAL (6,2),
@Inches_IntegerINTEGER,
@Height_FormattedVARCHAR (6)

SET @Feet = CAST(@Height AS INTEGER)

SET @Inches = ((@Height - @Feet) * 100)

IF (@Inches > 11) SET @Inches = (@Inches / 10)

SET @Inches_Integer = CAST(@Inches AS INTEGER)

SET @Height_Formatted = CAST(@Feet AS VARCHAR) + '''' 

SET @Height_Formatted = @Height_Formatted + CAST(@Inches_Integer AS VARCHAR) + '"'

RETURN @Height_Formatted

END

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating