• Esse (unregistered)

    If you are going to do it dirty, at least use a loop and create dynamic SQL statements.

  • Kriis (unregistered)

    Wow... just... wow

  • Not a code monkey (unregistered)

    You can do this in one line in Haskell

  • Kriis (unregistered) in reply to Esse
    Esse:
    If you are going to do it dirty, at least use a loop and create dynamic SQL statements.

    Loop? This saves the processing power required to do that. Talk about optimized. I want to learn from this guy.

  • Tim (unregistered) in reply to Kriis
    Kriis:
    Wow... just... wow
    likewise
  • Warren (unregistered)

    If you even start writing this without thinking "there must be a better way", you're not a programmer, just a typist that knows SQL.

  • Some Jerk (unregistered)

    Ugggh! Blah Blah Blah : Paid by the line Blah Blah Blah : The real WTF is <Insert Ignorant Statement Here>

    Yes... this is someone you should really learn from. If you can master this talented individuals' skills, you may even be able to work for Able's Car Wash writing a foxpro database to total daily inclome!

  • Some Jerk (unregistered) in reply to Warren
    Warren:
    If you even start writing this without thinking "there must be a better way", you're not a programmer, just a typist that knows SQL.

    Correction... a typist who happens to know a website that displays sql code.

  • Ozzy (unregistered)

    TLDR

    captcha: minim (oh the irony)

  • Andrew (unregistered)

    At least he used temp tables.

  • Sreejith K. (unregistered) in reply to Warren
    Warren:
    If you even start writing this without thinking "there must be a better way", you're not a programmer, just a typist that knows SQL.

    I agree.

  • John (unregistered) in reply to Tim
    Tim:
    Kriis:
    Wow... just... wow
    likewise

    My reaction was "Oh god, please no", followed by "no, no, no, no..." as I continued to scroll down.

  • Matyas (unregistered)

    Somebody please shoot me now! Reading through this is causing me so much pain...

  • (cs)

    Who has time to write all this shit? PBTL?

    Edit: On second look, I see no ;'s. Is that all 1 statement? Jesus.

  • iToad (unregistered)

    String processing with SQL. What could possibly go wrong?

  • Gary (unregistered) in reply to Not a code monkey
    Not a code monkey:
    You can do this in one line in Haskell
    You can do it in one line in a lot of languages. More to the point, you can probably do it in ~1 line of SQL, if you let that line get very long.

    Breaking these up for readability:

    select left(lname, case charindex(' ', lname) when 0 then len(lname) else charindex(' ',lname)-1 end) as part1,
    right(lname, len(lname) - charindex(' ', lname)) as part2,
    lname 
    from nametable
    

    Limiting this to looking at the last four characters would be trivial.

  • Chronomium (unregistered) in reply to iToad
    iToad:
    String processing with SQL. What could possibly go wrong?
    Indeed, this is clearly one of those cases where you're using a screwdriver to mix cake batter.
  • (cs) in reply to Chronomium
    Chronomium:
    iToad:
    String processing with SQL. What could possibly go wrong?
    Indeed, this is clearly one of those cases where you're using a screwdriver to mix cake batter.
    This is mixing cake batter using another cake.
  • russianyaz (unregistered)

    Whaaat?!??!

    [image]
  • dguthurts (unregistered)

    TRWTF is that first name, last name, and initials are not separate fields in the table.

    Gawd. I wonder how this mess would handle Mr. John Smith, Jr. DDS?

  • Valrandir (unregistered) in reply to iToad

    This can't be real

  • (cs) in reply to Zylon
    Zylon:
    This is mixing cake batter using another cake.
    Looks a lot like it's being mixed with a whole bonus bakery.
  • refoveo (unregistered)

    My mouse scroll wheel just gave me a blister.

  • Some Jerk (unregistered) in reply to refoveo
    refoveo:
    My mouse scroll wheel just gave me a blister.

    My Krapometer redlined

  • Herwig (unregistered)

    Just GREAT!

  • (cs)

    This one is making my eyeballs bleed.

  • (cs)

    Hey, at least we now know what knives are for.

    Stabbing idiots like this, that's what.

  • (cs) in reply to Esse

    I don't know why but this looks like auto-generated code, it has to be auto-generated code, only an algorithm can be this DUMB!

    From this we can conclude that the surname table column is VARCHAR(54) right? Luckily it was VARCHAR(255).

  • Disguntled DBA (unregistered)

    Some of the lesser sins that would merely have his code bounced in code review:

    1. Ordering on insert into a temp table (WHY??)
    2. Casting join columns (Way to use indexes, dumbass)

    In this case, however, I think the correct course of action is job retraining. Preferably something involving paper hats, a fryolator, and a requirement that he wear a badge that reads "TRAINEE" for the first year.

  • Andrew (unregistered)

    Other people, when confronted with a problem like this think "Gee, I'll use regex!"

    Oh look, a problem sanely solved.

  • (cs)

    String Parsing is really not a good idea in SQL. Its a performance killer. This looks like something that would be better suited in the application code. If a lot of string parsing has to be done in sql, its a candidate for SQLCLR.

    That being said. There is a much better way to do this...

  • Pablo Lerner (unregistered) in reply to iToad

    I think just the string processing

  • But (unregistered) in reply to Andrew
    Andrew:
    At least he used temp tables.
    That could actually make it worse. If this is SQL Server (which it looks like) then temporary tables are created in tempdb, which means that the script will fail if the account running it doesn't have Create Table permissions in tempdb.
  • Grzechooo (unregistered)

    Hmm, I ain't a SQL expert, but dealing with strings is something on app side, not on SQL side, am I right?

  • Some Jerk (unregistered) in reply to Grzechooo

    Depends on the circumstances. There are better ways to do it on the SQL end as well. Under the circumstances however, given that it only transforms the output, it would definately be better to do this on the app end.

  • dc (unregistered) in reply to Warren
    Warren:
    If you even start writing this without thinking "there must be a better way", you're not a programmer, just a typist that knows SQL.

    But on the plus side, they passed their typing final with flying colors! 666 words per second?

  • YR (unregistered)

    I could claim this is bullshit and the sender faked it... but I doubt he'd go into the trouble of creating this, so what I can say is...

    ... take this expert, and tie him. And bury him. Waist below into an ant's nest. Under a hornet's nest. And kick the hornets. Please.

  • Demon (unregistered) in reply to iToad

    This isn't a query, it's a war declaration!

  • Some Jerk (unregistered) in reply to Demon
    Demon:
    This isn't a query, it's a war declaration!

    It is what happens when you attempt to create Artificial Intelligence from Authentic Stupidity.

  • (cs)

    One word: OMG!

  • coward (unregistered) in reply to iToad

    Exactly this

  • (cs)

    What an idiot. He should've used table variables.

  • persto (unregistered)

    God forbid writing a function that splits strings by space characters (while maybe uniforming space characters first).

  • TK (unregistered) in reply to Kriis
    Kriis:
    Wow... just... wow

    That's all I could say when I was looking at the code. This is an epic WTF

  • Evan (unregistered) in reply to Chronomium
    Chronomium:
    iToad:
    String processing with SQL. What could possibly go wrong?
    Indeed, this is clearly one of those cases where you're using a screwdriver to mix cake batter.
    I've used pliers to eat a pancake before.

    (It was a camping trip and we had a shortage of silverware because we're stupid, but I had my Leatherman.)

  • Wow... (unregistered) in reply to iToad

    I'm no expert either, but I'm speechless... By the way, cool name iToad...

  • Ignatz (unregistered)

    Any fule no you have to use XML for really enterprisey string manipulation in SQL.

    SELECT XmlName.value('FirstName[1]', 'varchar(30)'), XmlName.value('LastName[1]', 'varchar(30)') FROM ( SELECT XmlName = CAST('<FirstName>' + REPLACE(clinician_name, ' ', '</FirstName><LastName>') + '</LastName>' AS XML) FROM ( SELECT clinician_name = 'Bugs Bunny' UNION ALL SELECT 'Fred Flintstone' UNION ALL SELECT 'Barney Rubble' ) names ) split

    Let values with anything other than exactly one space character be an exercise for the student.

  • Not at all (unregistered)

    Loop unrolling always gives you that critical performance boost.

  • myName (unregistered)

    The real WTF is posting all of it without any snipping.

  • Keith (unregistered) in reply to Chronomium

    This leads to a delicious vodka and orange infused cake.

Leave a comment on “SQL Splits”

Log In or post as a guest

Replying to comment #:

« Return to Article