Comment On De-evolutional Theory

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 ... [expand full text]
« PrevPage 1Next »

Re: De-evolutional Theory

2005-02-14 12:53 • by icelava
Is there a CalculatePriceOfCar() UDF that returns $1.11 too? I'd love to buy from this system.

Re: De-evolutional Theory

2005-02-14 12:56 • by fluffy
What's wrong with just storing the number as a float or fixed-point decimal or whatever?

Re: De-evolutional Theory

2005-02-14 12:57 • by
Regarding the logo voting: The URL for the 6th logo has the same URL as the 5th option.

Re: De-evolutional Theory

2005-02-14 13:06 • by Mike R
One would think if they were about to give up, they'd just return @MoneyAmount.



I'd take a stab at pointing out a short and proper implementation, but,
I have no idea what the original intent of the function may have
been.  Why would someone want to change 1.00 to 1, leave 1.100 as
1.100 and 1.11 as 1.11?



I mean if the intent were to return 1.00 as 1, 1.100 as 1.1 and 1.11 as 1.11, you'd think they'd do something like  cast(cast('1.0' as float) as varchar(10))

Re: De-evolutional Theory

2005-02-14 13:08 • by
29653 in reply to 29650
:
Regarding the logo voting: The URL for the 6th logo has the same URL as the 5th option.



If you want to vote for 6, then this should work...

Re: De-evolutional Theory

2005-02-14 13:09 • by JamesCurran

So, is this what they wanted....


CREATE FUNCTION ufTrimZeros_JMC (@MoneyAmount varchar(15)) 
RETURNS varchar(15) AS 
BEGIN
DECLARE @ma2 varchar(18)
-- Assumes the near certainty that the string !@! does not appear in the
-- @moneyAmount to start with
 SET @ma2 = @MoneyAmount + '!@!'
 SET @ma2 =  REPLACE( @ma2, '.00!@!', '!@!')
 SET @ma2 =  REPLACE( @ma2, '.0!@!', '!@!')
 SET @ma2 =  REPLACE( @ma2, '.!@!', '!@!')
 SET @ma2 =  REPLACE( @ma2, '!@!', '')
 
 RETURN @ma2
END

Re: De-evolutional Theory

2005-02-14 14:26 • by Jeff S
Formatting should never be done by the database layer.  This is horrible. 

 

Obviously, they gave up on this function and aren't using it, which is good, so hopefully someone smartened up and said "you know, we could do formatting at the ... I don't know ... PRESENTATION layer?! "

 

People who write functions like this are the same people who wonder why dates and dollar amounts don't sort correctly in their SQL statements.  ("why does the stupid computer think my varchar value of $9.20 is larger than $123.43 ?   Or that 1/2/2004 comes BEFORE 5/12/2002?  Stupid computers!")

Re: De-evolutional Theory

2005-02-14 15:07 • by fuali
well, I bet this is a helluva performance increase!

Re: De-evolutional Theory

2005-02-14 15:23 • by Bustaz Kool
29660 in reply to 29657
Jeff S:

Formatting should never be done by the database layer.  This is horrible. 

 


Amen, brother... Testify!!!

Re: De-evolutional Theory

2005-02-14 16:06 • by Blue
29669 in reply to 29660
I particularly like the pinky-finger / inkpad thing.  Subtle way
of saying you didn't go out of your way to prevent cheating.



I'm guessing some will happen, given my experience in the online gaming
world, and observation of emerging democracies (hell, even
long-established ones!)



Re: De-evolutional Theory

2005-02-14 16:09 • by Mike Dimmick
29671 in reply to 29660

All the messing around with REPLACE is crazy. It wastes memory. Instead you should start at the right-hand end of the string, gather the contiguous set of zeros, then check if there's a decimal point to the left of the zeros. If there is, trim off the zeros using SUBSTRING. If not, do nothing - the zeros are significant.


I'd say it's crazy to do this in the database, but I know of a product that forces it (or so it seems) due to its lame formatting capabilities: Crystal Reports.


Sample code, if you really must:

[code language="tsql"]

DECLARE @MoneyVariable varchar(15)
SET @MoneyVariable = '1.1100000'


DECLARE @ret varchar(15)


DECLARE @firstZero int
SET @firstZero = LEN(@MoneyVariable) + 1


WHILE @firstZero > 0
BEGIN
    IF
SUBSTRING(@MoneyVariable, @firstZero - 1, 1) <> '0'
BREAK
    SET @firstZero = @firstZero - 1
END


-- @firstZero now points to the first zero. Check if there's a
-- decimal
point before this point


DECLARE @p int
SET @p = @firstZero - 1


DECLARE @found bit
SET @found = 0


WHILE @p > 0
BEGIN
    IF SUBSTRING(@MoneyVariable,
@p, 1) = '.'
    BEGIN

        SET @found =
1
        BREAK
    END
    SET @p = @p - 1
END


IF @found = 1
    SET @ret = SUBSTRING(@MoneyVariable, 1,
@firstZero - 1)
ELSE
    SET @ret = @MoneyVariable


SELECT @ret

[/code]

Re: De-evolutional Theory

2005-02-14 16:35 • by Jeff S
29674 in reply to 29671
Mike Dimmick:

I'd say it's crazy to do this in the database, but I know of a product that forces it (or so it seems) due to its lame formatting capabilities: Crystal Reports.



Huh ?  We have our first "WTF" comment of the day!


Obviously, like almost ALL presentation layer applications, Crystal will automatically trim trailing zeroes from numbers -- assuming you actually return a correct (numeric) datatype.  And, of course, if you want to format the number any other way (or have crystal convert the varchar to a numeric type) it is pretty easy to do if you know how read the manual or the help files.


Please, for your sake and the sake those who someday will inherit the code you write, learn how to use a product before you start calling it "lame" and begin inventing horrible unnecessary "work-arounds" such as coding functions like this.

Re: De-evolutional Theory

2005-02-14 19:10 • by EWilson

I agree with other comments that this should (generally) be a presenation tier issue. However, some of the suggested "solutions" are also not much better than the original failed attempts, IMHO. This seems simple and efficient:


return left(@MoneyVariable, patindex('%[^.0]%', reverse(@MoneyVariable)))


(Find the right-most non-0 and non-decimal-point character, and give me back everything left of that.)


Sometimes the data tier is the *only* tier (as in data-warehousing and data extracts) and it's handy knowing what little tricks T-SQL has to offer.

Re: De-evolutional Theory

2005-02-14 19:16 • by
29681 in reply to 29671
Dear non-denominational-deity -- I think the code in that comment is actually worse than the original WTF!  Well, ok, maybe only almost worse.  It's hard to tell, but it's right up there anyway.

Re: De-evolutional Theory

2005-02-14 19:18 • by EWilson
29682 in reply to 29680
(One addendum... this would leave the decimal there if it was all zeros to the right. This could be wrapped in similar logic to trim the right-most character if it ended being a decimal point. It can still stay a one-liner, which SQL Server can better "inline" into queries for performance.)

Re: De-evolutional Theory

2005-02-14 20:50 • by JamesCurran
29683 in reply to 29680
EWilson:
This seems simple and efficient:

return left(@MoneyVariable, patindex('%[^.0]%', reverse(@MoneyVariable)))


"simple and efficient" and well, wrong. First of all, patindex will return the index into the reversed string, while left() needs the index into the forward string, but thats simple enough to fix with a "len(@MoneyVariable) - " in there.


But the real problem is that it doesn't stop at the decimal.  Given the string "120.00", yours would reduce that to just "12"

Re: De-evolutional Theory

2005-02-14 23:40 • by Son of Guayo
29695 in reply to 29680
EWilson:


(Find the right-most non-0 and non-decimal-point character, and give me back everything left of that.)






Well, this would trim 1,000,000 down to 1 - at least the original would only trim it down to 1.11 :)

Re: De-evolutional Theory

2005-02-14 23:59 • by Jon Limjap
I think the function should have been named "I_Give_Up" [:P]

Re: De-evolutional Theory

2005-02-15 00:31 • by EWilson
29697 in reply to 29695

Well, this would trim 1,000,000 down to 1 - at least the original would only trim it down to 1.11 :)
[/quote]


Yes, it unfortunately assumed an incoming string *with* a decimal. The point was not to deliver a full-fledged function but to illustrate that there was (probably) a solution that could be done in one line. (I think it's safe to say if anyone uses code verbatim from a web-site, esp. a forum, that's a major WTF in itself, right?)


But... lesson learned: don't post things typed up in 90 seconds with no testing while trying to simultaneously put on coat to get out the door for a doctor's appointment. :)

Re: De-evolutional Theory

2005-02-15 00:35 • by EWilson
29698 in reply to 29695

[Prior reply mangled in forum for some reason...]


Son of Guayo:


Yes, it unfortunately _ass_umed an incoming string *with* a decimal. The point was not to deliver a full-fledged function but to illustrate that there was (probably) a solution that could be done in one line rather than N pages. (I think it's safe to say if anyone uses code verbatim from a web-site, esp. a forum, that's a major WTF in itself, right?)


But... lesson learned: don't post things typed up in 90 seconds with no testing while trying to simultaneously put on coat to get out the door for a doctor's appointment. :)

Re: De-evolutional Theory

2005-02-15 02:32 • by

Another question about the logo's.


Does 03 13 37 have some special meaning??

Re: De-evolutional Theory

2005-02-15 06:21 • by
29704 in reply to 29700
here we go...finest hack0r slang:

http://en.wikipedia.org/wiki/Leet

Re: De-evolutional Theory

2005-02-18 17:53 • by
30025 in reply to 29680
EWilson:

I agree with other comments that this should
(generally) be a presenation tier issue. However, some of the suggested
"solutions" are also not much better than the original failed attempts,
IMHO. This seems simple and efficient:


return left(@MoneyVariable, patindex('%[^.0]%', reverse(@MoneyVariable)))


(Find the right-most non-0 and non-decimal-point character, and give me back everything left of that.)





So, for an input value of

12300000.00



we could expect an output value of

123



???

Re: De-evolutional Theory

2008-03-29 13:38 • by Chris (unregistered)
Could you just do something like this?

DECLARE @retVal int
SET @retVal = SELECT cast(@floatVal as Integer)
return @retVal

Rather than complex Regex statements? I mean, that's if you REALLY need to format something here as opposed to in your actual presentation layer.

(please excuse any syntax errors, don't do much T-SQL work)

Re: De-evolutional Theory

2009-12-02 12:06 • by Ben (unregistered)
The "keep all old/irrelevant code, just comment it out" method of version control is one I'm very familiar with in some of the legacy code I maintain.

Re: De-evolutional Theory

2011-01-11 06:57 • by cindy (unregistered)
« PrevPage 1Next »

Add Comment