First and foremost, please take a few seconds to vote for The Daily WTF logo. No login or anything required, just click on your favorite choice. Now back to the regularly scheduled program ...

It can be quite excitingto glimpse at the change history of a method and see how it has evolved over time. Well exciting is probably too strong of a word for most of you; perhaps "boring," "tedious," or "the worst-possible-way-I-can-think-of-spending-my-free-time" is a more apt description. No less, I think this example from Nathan Ratcliff provides a unique glimpse of the ... de-evolution ... of the ufTrimZeros() T-SQL function with its three failed attempts at trimming zeros. Hopefully 1.11 should be the only number ever needed ....

CREATE FUNCTION dbo.ufTrimZeros (@MoneyAmount varchar(15))  
RETURNS varchar(15) AS  
BEGIN 
 RETURN '1.11'
 
/* 
 SET @MoneyAmount =  REPLACE( REPLACE( @MoneyAmount, '.00', ''), '.0', '')
 
 IF RIGHT(@MoneyAmount, 1) = '.'
  SET @MoneyAmount = REPLACE(@MoneyAmount, '.', '')
 RETURN @MoneyAmount
*/
/*
 DECLARE @StrippedZeros AS varchar(15)
 DECLARE @Temp1 as varchar(15)
 DECLARE @Temp2 as varchar(15)
 SET @Temp1 = Substring(@MoneyAmount, 1, charindex('.', @MoneyAmount)-1)
 SET @Temp2 = Substring(@MoneyAmount, charindex('.', @MoneyAmount) + 1, Len(@MoneyAmount) - (charindex('.', @MoneyAmount) + 1))
 
 DECLARE @I int
 SET @I = Len(@Temp2)
 WHILE @I > 0
 BEGIN
  IF Substring(@Temp2, @I, 1) <> '0'
   Break;
  SET @I = @I - 1
 END
 IF @I > 0 
  SELECT @StrippedZeros = @Temp1 + '.' + Substring(@Temp2, 1, Len(@Temp2) - @I)
 ELSE
  SELECT @StrippedZeros = @Temp1
 
 RETURN @StrippedZeros
*/
/*
 DECLARE @StrippedZeros AS varchar(15)
 
 SET @StrippedZeros = REPLACE( CAST( @MoneyAmount AS varchar( 12 )), '.00', '' )
 
 SELECT @StrippedZeros = 
  CASE
   WHEN RIGHT( REPLACE( CAST( @StrippedZeros AS varchar( 12 )), '.00', '' ), 1 ) = '0' 
   THEN LEFT( REPLACE( CAST( @StrippedZeros AS varchar( 12 )), '.00', '' ), LEN ( REPLACE( CAST( @StrippedZeros AS varchar( 12 )), '.00', '' ) ) - 1 )
   ELSE @StrippedZeros
  END
 
 RETURN @StrippedZeros
*/
END

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!