- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's cr@p like this that reminds me why I prefer to use DB/2 and Microsoft SQL Server.
Admin
Dang, almost made first reply.
RPAD ('', 9, ' '); was apparently too easy.
Admin
I'm sure there's some WTFers out there that can abuse those in equal degrees.
What's up with special cases for 1 and 2 space pads? Bizarre.
Admin
Oracle...........more like............Boracle....................right?
Admin
NO I WILL NOT MAKE OUT WITH YOU!
This girl wants to make out with me in the middle of class!
Admin
I don't write any functions inside SQL/Oracle queries, so this one took me a few minutes to figure out. Then I realized it's because I don't have nearly enough brain damage for this to make sense. Our "programmer" made a function that recreates the functionality of a built-in function but inexplicably gives special circumstances to a few situations depending on the parameter passed to it.
This is especially funny because RPAD seems to pad spaces on by default, so there is no reason to create a wrapper function that uses the space as the parameter to pass to RPAD. Unless ya got the dain brammage, that is.
Admin
Dumb question from someone who is certainly not a brillant database genius... Is it normal for everything except variable names to be in upper case? That is really bad netiquette (codiquette?) if you ask me....
Admin
it's....it's beautiful..........
Admin
This code is brilliant. It optimizes out the function RPAD, which is known to be slow, with faster code for one or two char strings.
Admin
Yes, it is typical for SQL reserved words / built-in functions to be in upper case, and database / table / column names to be in mixed case. That's the style in MS SQL Books Online, anyway. But it's a rule most often observed in the breach.
Admin
I know ... before yesterday this code looked like
IF SPACES = 1
THEN
RETURN PAD;
ELSIF SPACES = 2
THEN
RETURN PAD || PAD;
ELSIF SPACES = 3
THEN
RETURN PAD || PAD || PAD;
ELSIF SPACES = 4
THEN
RETURN PAD || PAD || PAD || PAD;
....
ELSIF SPACES = 99
THEN
RETURN PAD || PAD || ..|| PAD||PAD || PAD || PAD;
But thanks to yesterday's WTF State Management post, someone actually tried to clean up this code getting rid of nearly all those if statements. That has to be what is going on here.
Admin
I love it. "I'm gonna call this custom function to do the right-padding. And just in case it doesn't pad it with the right number of spaces, I'm going to call the built-in function to do the right-padding all over again."
Translation: "Sometimes this RPAD_SPACES function works, and sometimes it doesn't, but I'll be damned if I spend the 30 minutes it would take to figure out why. So let me just do this double-checking thing and be done with it. Sure it's going to increase maintenance time in the future, but I am a highly-paid consultant and I will be long gone by then."
Admin
The world definitely doesn't have enough complex padding algorithms. And if it does, what's one more? Probably one of those Oracle consultants, just padding his/her check.
Admin
Wait, it's worse than I thought! It's calling the RPAD_SPACES function when the value is NULL. So they could have just hardcoded a string of nine or fifteen spaces, and not even called RPAD_SPACES in the first place.
Admin
Admin
<font size="2">
True. I've seen christoofar's SQL...*shiver*</font>
Admin
No way dude, you never know when 'space' will change.
Admin
Without checking I couldn't say exactly how RPAD handles nulls but I know that
Admin
>>
Without checking I couldn't say exactly how RPAD handles nulls but I know that
Admin
Upper-case SQL always seemed like a throwback to the COBOL and FORTRAN days to me; after all, they are contemporaries. Weird how that particular habit has survived the lower_casing and then CamelCasing of every other language, even if it is slowly dying.
Admin
Admin
Here's a question for you, don't ponder it too hard...
How many drugs would you have to take, over what period of time, for this loop to actually make any sense?
FOR LOOPVAR IN 1 ..SPACES - 2
LOOP
PADVAR := PAD || RPAD ( PAD
, LOOPVAR + 1
, ' ' );
END LOOP;
RETURN PADVAR;
Admin
Admin
It's actually the recommend way of doing it. Studies have shown that UPPERCASE keywords are much easier to read ...
<FONT face="Courier New">SELECT col1, col2
FROM sometable
WHERE col3 = 'asdf'
AND col4 IS NULL</FONT>
Admin
FOR LOOPVAR IN 1 ..SPACES - 2
LOOP
PADVAR := PAD || RPAD ( PAD
, LOOPVAR + 1
, ' ' );
END
LOOP;
RETURN PADVAR;
So, let me see if I have this straight - if I call RPAD_SPACES(99) (assume I have a head injury or whatever), I have the satisfaction of knowing that it constructed the return values for RPAD_SPACES(3) through RPAD_SPACES(98) (using RPAD, 'natch) and promptly tossed them aside. Brillant!
Admin
I've only worked with Oracle version 8 and higher and I guarantee that '' <> NULL.
Admin
In Oracle, this is unfortunately true (at least in the versions I know) and these NULL strings can give you many happy debugging hours, because
length('') is not 0, but NULL
lpad('', 5) is not ' ' but NULL
rpad('', 5) is not ' ' but NULL
Admin
Maybe there are some settings to make '' <> NULL, but in a default install of anything from Oracle 6 to Oracle 9i, '' is NULL
Admin
I just need to say - it burns, it really, REALLY REALLY burns. This reminds me why I hate Oracle.
Admin
"It's actually the recommend way of doing it. Studies have shown that UPPERCASE keywords are much easier "
That's why we have syntax highlighting now :) Probably just as quick to notice a color as it is to notice UPPERCASE, and it has the upshot that it doesn't make your code look like it was pulled from a vault circa the Nixon administration.
Admin
Since I asked you for a link, I guess it's only fair to give one to support my view also... http://www.tc-forum.org/topicus/ru28theu.htm
Admin
I don't know about Oracle, but in MS SQL, NULL <> NULL pi
Admin
UPPERCASE or lowercase is just a matter of personal style in PL/SQL, since PL/SQL is not case sensitive. Anyway, some tools like TOAD have a tendency to force their opinion about "why UPPERCASE is so much better" to innocent programs.
Admin
Upper-case SQL is useful for a couple of reasons. As mentioned already, it makes keywords stand out, and it makes it very easy to differentiate between keywords and table and field names (as these are usually lowercase). Finally, it is also useful because SQL frequently appears mixed in with other programming languages, so it makes it easier to tell when you're looking at code that will execute on the server, and code that is part of your application. Imagine if T-SQL looked just like C.
Admin
Actually, no, you won't have that satisfaction.
Exercise for the student: Why not?
Admin
this is almost a wtf on its own, '' is null is documented
SQL> select * from v$version;
BANNER
Oracle Database 10g Release 10.1.0.3.0 - Production PL/SQL Release 10.1.0.3.0 - Production CORE 10.1.0.3.0 Production TNS for Linux: Version 10.1.0.3.0 - Production NLSRTL Version 10.1.0.3.0 - Production
SQL> select nvl('', 'The empty string is NULL') wtf from dual;
WTF
The empty string is NULL
Admin
Yes, the fifth dentist recommends it!
dZ.
Admin
Don't you mean Brillant?
dZ.
Admin
well done !
Admin
I seem to have misplaced my references. They were in SQL PROGRAMMING STYLE by Joe Celko. I used to do lower case with syntax hilighting, but he had good evidence from recent studies (1997 I think) which demonstrated that SQL code with the upper case keywords was easier to read, understand, and therefore less error prone.
Consider that, especially when it comes to SQL, syntax hilighting is less avaiable than other languages. SQL Strings in applications for example.
Admin
Admin
I can get behind that. It doesn't work for me, but if it's better on average... Personally, I find too many letters in a row with straight lines and the same metrics blends into line noise for me; lowercase offers more variety for the pattern-matching, even without color. I don't know eclipse highlights, haven't used sql in it yet, but I think phped did and I know textpad and editplus don't.
On the other hand the whole second half of that is uppercase, I guess he was never taught that sql variables aren't keywords. =p
I love your spelling of highlightning. I know it's becoming more common, but it always makes me think of h-dawg and his buddy's hilita fix. The onion used to be so cool. =D
Admin
Because those values, once calculated, are concatenated onto the working value, not tossed aside. Each calculation is an integral part of the final result.
Admin
You guys think style guides reccomending uppercase are a WTF? Wait until you've programmed in Oberon. Since it's case sensitive, Wirth (yes, the same guy who invented Pascal) had the 'brillant' idea to make keywords all uppercase. Imagine tapping CAPS LOCK every 7 characters on average. I gave up after a few hours. I mean, I like to try new things, but that was too much.
Remember: things can always, ALWAYS get worse.
Admin
If you do that in PL/SQL, also writing the PL/SQL keywords (like FUNCTION, IF, WHILE etc.) in uppercase, you end up with about 60% of all words in uppercase, which is a PITA to read.
Some editors, e.g. Slickedit, provide SQL syntax highlighting for SQL code embedded in C.
Admin
My eyes have no cones you insensitive clod!
Admin
In most RDBMSs (Oracle, MS SQL, MySQL, PostgreSQL, Ingres) null <> null. Oracle is the only one I've seen where isnull('') = true. It's great (NOT) when you're using trim(firstname) = trim(:c-variable) in your where clause and wondering why your counts are incorrect.
Admin
The ''=NULL thingy has a "nice" side effect in Java: of course, the JDBC driver will give you a null pointer instead of an empty string for columns containting ''. A good oportunity to test how the system handles null pointer exceptins.
Admin
Bzzt, wrong:
The only assignment is to PADVAR. PADVAR does not occur on the right hand side of the assignment.
Admin
Did anyone else check this link out?
I especially like the bit where he says "both my great grandparents". I guess those red necks have close families