• (cs) in reply to Volmarias

    public class WTFGenerator {

    public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
    } catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits <= 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i < max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }

    // if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad < 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i <= numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad > 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i <= digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }

    }//end class

    Volmarias, I think this is a bit sad, doing all this function just for generating a copy&paste code :)

  • (cs) in reply to Enric Naval
    Enric Naval:
    public class WTFGenerator {

    public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
    } catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits <= 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i < max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }

    // if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad < 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i <= numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad > 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i <= digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }

    }//end class

    Volmarias, I think this is a bit sad, doing all this function just for generating a copy&paste code :)



    args[1] is the second argument

  • (cs) in reply to Enric Naval

    uh? half of my code has disappeared! Damn the < tag!

    public class WTFGenerator {      public static void main(String[] args) {      int numberOfDigits;     try {       numberOfDigits = Integer.parseInt(args[1]);            }     catch (java.lang.NumberFormatException ex) {       System.out.println("The first argument has to be an integer");       System.exit(-1);     }     if ( numberOfDigits <= 0 ) {       System.out.println("The first argument has to be a POSITIVE integer");       System.exit(-2);     }      long max = numberOfDigits * 10;     for(long i = 1; i < max ; i++ ) {        System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'");        System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'");      }    } 

      // if num is too short, return num adding zeros at the start    // if num is length OK, return num   // if num is too long, return num shortened to adequate length and all digits set to zero   public String padNumber (long x, int numberOfDigits) {      String num = Long.toString(x);     int digitsToPad = numberOfDigits - num.length();     if ( digitsToPad < 0 ) { // too long       StringBuffer temp = new StringBuffer(numberOfDigits);       for ( int i = 0; i <= numberOfDigits; i++ ) {         temp.append("0");       }       num = temp.toString();     }     else if ( digitsToPad == 0 ) { //length OK       //num = num;//NOOP     }     else if ( digitsToPad > 0 ) { //too short       StringBuffer temp = new StringBuffer(numberOfDigits);       for ( int i ; i <= digitsToPad ; i++ ) {         temp.append("0");       }       temp.append(num);       num = temp.toString();     }     return num;   } 

    }//end class

  • (cs) in reply to Mung Kee
    Mung Kee:


    args[1] is the second argument

    Oppps, I was thinking in bash, where $0 is the script name in the command line, and $1 is the first argument...

  • (cs) in reply to Enric Naval

    hum, what about the pre tag? It works, doesn't it?

    public class WTFGenerator { 
    

    public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
    } catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits LESS OR EQUAL TO 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i LESS OR EQUAL TO max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }

    // if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad LESS THAN 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i LESS OR EQUAL TO numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad GREATER THAN 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i LESS OR EQUAL TO digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }

    }//end class

  • (cs) in reply to Enric Naval

    thisForumSoftwareHaters++;

  • SDL (unregistered)

    in ansi sql:

    select substr(to_char(to_number(:sequenceCode)+1),'099'),1,2) from ...

  • Masklinn (unregistered) in reply to Suomynona
    Anonymous:
    As people continued to complain that the preview didn't always look like the final result, the author of the forum software just dropped the preview button entirely (at least for unregistered users). What a brillant solution! Frankly, by now I'm surprised that the URL in my browser's location bar does not contain snippets of SQL.

    Wouldn't be possible, since even the frigging page number is transferred via POST instead of GET (which means that you can't reload the page without getting some retarded "hey mate I have to resend informations to the server, are you ok with that?" popup from your browser...

    Enric Naval:
    Volmarias, I think this is a bit sad, doing all this function just for generating a copy&paste code :)

    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    AND you can leave the generator somewhere in case you need to "extend" the feature/code !

  • (cs) in reply to Enric Naval

    [blockquote] [pre] public class WTFGenerator {

    public static void main(String[] args) { int numberOfDigits; try { numberOfDigits = Integer.parseInt(args[1]);
    } catch (java.lang.NumberFormatException ex) { System.out.println("The first argument has to be an integer"); System.exit(-1); } if ( numberOfDigits LESS OR EQUAL TO 0 ) { System.out.println("The first argument has to be a POSITIVE integer"); System.exit(-2); } long max = numberOfDigits * 10; for(long i = 1; i LESS OR EQUAL TO max ; i++ ) { System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'"); System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'"); } }

    // if num is too short, return num adding zeros at the start // if num is length OK, return num // if num is too long, return num shortened to adequate length and all digits set to zero public String padNumber (long x, int numberOfDigits) { String num = Long.toString(x); int digitsToPad = numberOfDigits - num.length(); if ( digitsToPad LESS THAN 0 ) { // too long StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i = 0; i LESS OR EQUAL TO numberOfDigits; i++ ) { temp.append("0"); } num = temp.toString(); } else if ( digitsToPad == 0 ) { //length OK //num = num;//NOOP } else if ( digitsToPad GREATER THAN 0 ) { //too short StringBuffer temp = new StringBuffer(numberOfDigits); for ( int i ; i LESS OR EQUAL TO digitsToPad ; i++ ) { temp.append("0"); } temp.append(num); num = temp.toString(); } return num; }

    }//end class

    [/pre] [/blockquote]

    ( sorry for filling the forum with horribly formatted posts )

  • Masklinn (unregistered) in reply to Enric Naval
    Enric Naval:
    ( sorry for filling the forum with horribly formatted posts )

    I guess you wanted

    public class WTFGenerator {
        public static void main(String[] args) {
            int numberOfDigits;
            try {
                numberOfDigits = Integer.parseInt(args[1]);
            }
            catch (java.lang.NumberFormatException ex) {
                System.out.println("The first argument has to be an integer");
                System.exit(-1);
            }
            if ( numberOfDigits LESS OR EQUAL TO 0 ) {
                System.out.println("The first argument has to be a POSITIVE integer");
                System.exit(-2);
            }
            long max = numberOfDigits * 10;
            for(long i = 1; i LESS OR EQUAL TO max ; i++ ) {
                System.out.println("IF @sequenceCode = '" + padNumber(i,numberOfDigits) + "'");
                System.out.println("\tSET @sequenceCode = '" + padNumber(++i,numberOfDigits) + "'");
            }
        }
        // if num is too short, return num adding zeros at the start
        // if num is length OK, return num
        // if num is too long, return num shortened to adequate length and all digits set to zero
        public String padNumber (long x, int numberOfDigits) {
            String num = Long.toString(x);
            int digitsToPad = numberOfDigits - num.length();
            if ( digitsToPad LESS THAN 0 ) { // too long
                StringBuffer temp = new StringBuffer(numberOfDigits);
                for ( int i = 0; i LESS OR EQUAL TO numberOfDigits; i++ ) {
                    temp.append("0");
                }
                num = temp.toString();
            }
            else if ( digitsToPad == 0 ) { //length OK
                //num = num;//NOOP
            }
            else if ( digitsToPad GREATER THAN 0 ) { //too short
                StringBuffer temp = new StringBuffer(numberOfDigits);
                for ( int i ; i LESS OR EQUAL TO digitsToPad ; i++ ) {
                    temp.append("0");
                }
                temp.append(num);
                num = temp.toString();
            }
            return num;
        }
    }//end class 

    BTW you're not outputting "ELSE IF", but only "IF"s

  • (cs) in reply to Masklinn

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

  • (cs) in reply to JRG
    JRG:

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.

    http://www.tldp.org/LDP/abs/html/comparison-ops.html

  • (cs) in reply to Masklinn

    Yes, that's it. Can you tell how you did it?

  • (cs) in reply to Enric Naval
    Enric Naval:
    JRG:

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.

    http://www.tldp.org/LDP/abs/html/comparison-ops.html

    [erich@localhost erich]$ wtf="hello world"
    [erich@localhost erich]$ length=$(echo $wtf|wc -c)
    [erich@localhost erich]$ echo $length
    12
    [erich@localhost erich]$
    
  • (cs) in reply to msumerano

    <FONT face="Courier New" size=2>see, nobody has the big question here.  why the hell is this function needed?  in what context is it used?  HOW DID IT GET THERE GOSH DARN IT.</FONT>

  • (cs) in reply to Enric Naval

    Enric Naval:
    uh? half of my code has disappeared!

    <FONT face="Courier New" size=2>brillant.</FONT>

  • (cs) in reply to ammoQ
    ammoQ:
    Enric Naval:
    JRG:

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.

    http://www.tldp.org/LDP/abs/html/comparison-ops.html

    [erich@localhost erich]$ wtf="hello world"
    [erich@localhost erich]$ length=$(echo $wtf|wc -c)
    [erich@localhost erich]$ echo $length
    12
    [erich@localhost erich]$


    WTF... like a Pentium doing divisions... fast, but wrong.
    The right way to do it:

    [erich@localhost erich]$ wtf="hello world"
    [erich@localhost erich]$ length=$(echo -n $wtf|wc -c)
    [erich@localhost erich]$ echo $length
    11
    [erich@localhost erich]$
    
  • Masklinn (unregistered) in reply to Enric Naval
    Enric Naval:
    Yes, that's it. Can you tell how you did it?

    Yes. You have to create a post, then immediatly switch to HTML view (clic on the HTML tab at the bottom), and use

    your code
    ;

    The important thing is to remember switching to HTML right out of the bat, or you'll get a much fubared code when you will.

  • Masklinn (unregistered) in reply to Masklinn

    Gosh, that piece of crap managed to interpret my l33t escaped characters... let's try again

    edit: leet, can't even write HTML entities, that sorry excuse for an editor creates HTML from them...

    [blockquote][pre]your_code[/pre][/blockquote]

    Replace [ and ] by lt and gts.

  • (cs) in reply to Masklinn

    "I can confirm that it is also gone for registered users. "If you can't fix it, kill it"."

    cough

    This may be good news - the author of the code may be realising that there are problems, and maybe coming to the conclusion that they need fixing. I hope so.

    Or perhaps Alex disabled the button.

  • (cs) in reply to Masklinn

    The HTML tab, which, of course, <b>doesn't appear on Opera, so I have to swith to IE</b>. ouch!

  • (cs) in reply to Enric Naval

    The HTML tab, which, of course, doesn't appear on Opera, so I have to swith to IE. ouch!

  • (cs) in reply to RhythmAddict
    Anonymous:

    Sometimes my boss and I have conversations like this...

    me: It's a complicated program, there is a lot of business logic

    him:  A lot of if/else's, hunh?


    An all-too-true statement I read in some book (IIRC "Refactoring: Improving the Design of Existing Code"):

    "There is very little that is less logical than so called 'business logic'"

    Usually it's a huge bunch of special cases that ultimately exist because of marketing (such as allowing salesmen to offer "special conditions" to convince uncertain customers).

  • Masklinn (unregistered) in reply to Enric Naval
    Enric Naval:
    The HTML tab, which, of course, doesn't appear on Opera, so I have to swith to IE. ouch!

    Well, it's not *THAT* bad, I can't even get the frigging captcha to appear with Firefox, which mean that I can't even post if i'm not using MSIE (me thinks i'm going to create an account just for that, because it's getting more and more annoying)

  • (cs) in reply to ammoQ
    ammoQ:
    Enric Naval:
    JRG:

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.

    http://www.tldp.org/LDP/abs/html/comparison-ops.html

    [erich@localhost erich]$ wtf="hello world"
    [erich@localhost erich]$ length=$(echo $wtf|wc -c)
    [erich@localhost erich]$ echo $length
    12
    [erich@localhost erich]$


    WTF?

    <font size="4">$ wtf="hello world, i can count"
    $ echo ${#wtf}</font>

    RTFM! LOL, LMAO
  • (cs) in reply to Ytram
    Ytram:
    On a side note, I cannot believe that people actually use the word 'leverage' in actual conversation.


    You mean leverage the word 'leverage', right?


    I occasionally leverage my foot into someone's posterior.
  • (cs) in reply to joost
    joost:
    ammoQ:
    Enric Naval:
    JRG:

    Masklinn:
    While I don't know if it's worth the hassle in Java for an average programmer, it's much faster to write such a crapcode generator in Python/Ruby/Perl than actually writing the crapcode by hand... even taking the fubared corner cases in account.

    I use excel or Ultraedit to generate my copy/paste code...

    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.

    http://www.tldp.org/LDP/abs/html/comparison-ops.html

    [erich@localhost erich]$ wtf="hello world"
    [erich@localhost erich]$ length=$(echo $wtf|wc -c)
    [erich@localhost erich]$ echo $length
    12
    [erich@localhost erich]$


    WTF?

    <font size="4">$ wtf="hello world, i can count"
    $ echo ${#wtf}</font>

    RTFM! LOL, LMAO


    nice to know! guess you are right with that RTFM...
  • (cs) in reply to Suomynona
    Anonymous:
    Bustaz Kool:

    As far as I can discern, all of the one liners throw an error on invalid input.



    And that's a good thing, Mr. Spolsky.

    Well, now we're getting into my mantra, "What are the requirements?". 

    The original solution (the original WTF) was able to gracefully handle any input that did not conform to a two digit number and return the input intact.  Is that the requirement?  I can't tell from the info given.  All we really have is the de facto results of the code as given.

    It is, to me, better to validate the data and only operate on valid inputs and return the character string. 

  • (cs) in reply to joost
    joost:
    Alex Papadimoulis:
    [...] and is best enjoyed if you've never programmed anything in T-SQL in your life before. [...]

    SET @number = INT(@sequenceCode)
    IF @number > 0
    SET @number = @number + 1
    IF @number = 100
    SET @number = 0
    IF @number < 10
     SET @sequenceCode = '0' + STR(@number)
    ELSE
    SET @sequenceCode = STR(@number)
    DROP DATABASE



    Hahahaha

    I hope at least _one_ person tried this...

    Lol

       -dave-
  • (cs) in reply to Enric Naval
    Enric Naval:
    thisForumSoftwareHaters++;


    ++thisForumSoftwareHaters++

    Count me in on this as well...

         -dave-

  • (cs) in reply to Masklinn
    Anonymous:

    Wouldn't be possible, since even the frigging page number is transferred via POST instead of GET (which means that you can't reload the page without getting some retarded "hey mate I have to resend informations to the server, are you ok with that?" popup from your browser...

    Ah, the biggest WTF of all. Still, it could be worse - it could use session data to keep track of what page you're viewing (shudder)

  • (cs) in reply to ammoQ

    <FONT face="Courier New">Re: guess you are right with that RTFM</FONT>

    <FONT face="Courier New">In principle, yes, but the man page of bash is such a mess, you can't really blame anyone for missing 'obvious' things like getting the number of characters in a variable, or parameter in sh-speak. You have your positional parameters, parameter expansion, brace expansion, tilde expansion, arithmetic expansion and whatnot. This thing ${#var} is buried in parameter expansion. :-S Of course you have to find all this out in that crappy 'less' pager. </FONT>

  • (cs)
    My one liner:
    <FONT color=#006400>SET @sequenceCode = 
    CAST(RIGHT('00' + CAST((CAST(@sequenceCode AS INT) + 1) AS VARCHAR(5)),2) AS CHAR(2))</FONT>
    It works, it is type correct, and accounts for all stated cases.
  • Robert (unregistered) in reply to ColdPie

    @sequenceCode = @sequenceCode + 1

    Do I win?

    BZZZZZZZZZZZZZZZZT! Thank you for playing, but 99 becomes 0 in the original code, and values above 99 remain unchanged.

    I don't know this language, but in C++, it would be

    if (sequenceCode < 100)

                sequenceCode = (sequenceCode + 1) % 100

    to duplicate the exact functionality of the original.

  • (cs) in reply to Robert
    Anonymous:

    @sequenceCode = @sequenceCode + 1

    Do I win?

    BZZZZZZZZZZZZZZZZT! Thank you for playing, but 99 becomes 0 in the original code, and values above 99 remain unchanged.

    I don't know this language, but in C++, it would be

    if (sequenceCode < 100)

                sequenceCode = (sequenceCode + 1) % 100

    to duplicate the exact functionality of the original.



    Not exactly, since sequenceCode is a string and all values except "00", "01", ..., "99" stay unchanged.
  • ML (unregistered)

    Lovely. I guess '00' stays '00'. Glory!

     

    ML

  • (cs) in reply to ML
    Anonymous:

    Lovely. I guess '00' stays '00'. Glory!



    WTF, you are right!!!
  • Anonymous Coward (unregistered) in reply to res2

    "00 stays 00" -- not a special case, that's the bug in the original code that had to be fixed.

     

  • Anonymous Coward (unregistered)
    Alex Papadimoulis:
    	<FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'01'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'02'</FONT>
    	<FONT color=#000099>ELSE</FONT> <FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'02'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'03'</FONT>
    	<FONT color=#000099>ELSE</FONT> <FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'03'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'04'</FONT>
    	<FONT color=#000099>ELSE</FONT> <FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'04'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'05'</FONT>
    	<FONT color=#000099>ELSE</FONT> <FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'05'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'06'</FONT>
    	<FONT color=#000099>ELSE</FONT> <FONT color=#000099>IF</FONT> @sequenceCode = <FONT color=#990000>'06'</FONT>
    		<FONT color=#000099>SET</FONT> @sequenceCode = <FONT color=#990000>'07'</FONT>
    	<FONT color=#009900>-- snip --</FONT>

    Doesn't anyone see it?

    This was written by a SENIOR programmer.

    That guy first learned to code in COBOL, and his minions still suffer the consequences.

     
  • (cs) in reply to David
    Anonymous:

    Best.  Solution.  Evar.  Truly leveraging the full dynamic range of your core competencies as well as the available technology.

     

    On a side note, I cannot believe that people actually use the word 'leverage' in actual conversation.  Not even business speak, but real actual person to person colloquial conversation.  Apparently they're too good for the word 'use.'

    That's an extremely proactive definitization of  leveraging the word leverage in bizTalk

    How long did it take you to definitize you reply?

  • countrydave (unregistered)

    <FONT face="Courier New" size=2>SET @sequenceCode =
      CASE
        WHEN LEN(@sequenceCode) <> 2 OR ISNUMERIC(@sequenceCode) <> 1 THEN @sequenceCode
        WHEN @sequenceCode IN ('00', '99') THEN '00'
        ELSE REPLACE(STR(CONVERT(int, @sequenceCode) + 1, 2, 0), ' ', '0')
      END</FONT>

  • (cs) in reply to countrydave
    countrydave:
    <font face="Courier New" size="2">SET @sequenceCode =
      CASE
        WHEN LEN(@sequenceCode) <> 2 OR ISNUMERIC(@sequenceCode) <> 1 THEN @sequenceCode
        WHEN @sequenceCode IN ('00', '99') THEN '00'
        ELSE REPLACE(STR(CONVERT(int, @sequenceCode) + 1, 2, 0), ' ', '0')
      END</font>


    Great, I just learned there's a difference between programming and writing stored procedures. Way to shift my paradigm!
  • 1111111111111111111111111111111111111111111111111111111111111111 (unregistered) in reply to joost

    Test

  • (cs)

    Seeing as how everyone else has taken a shot at this, I thought I would throw in my own code. It will leave the value the same if it is not numeric, less than 1 or greater than 99. It will make the value "00" if it is equal to 99. Otherwise, it will increment the value by 1 and will also pad with zeros if the return value is less than 10.


    -- first - validate that the input is actually a number
    if isnumeric(@sequenceCode) = 1 --note, cannot leave off the "= 1" as T-SQL will not accept "if isnumeric(@sequenceCode)" as a valid boolean expression
        begin
            -- make a temp variable so we don't have to keep casting
            declare @sequenceCodeAsInt int
            set @sequenceCodeAsInt = cast(@sequenceCode as int)
            -- check value is in range (greater than zero and less than 100)
            if @sequenceCodeAsInt > 0 and @sequenceCodeAsInt < 100
            begin
                --increment value by one, cast to varchar and choose last two digits (which will be '00' for 100)
                set @sequenceCode = right(cast(@sequenceCodeAsInt + 1 as varchar),2)
                --left pad with '0' if length is less than 2
                set @sequenceCode = replicate('0',2 - len(@sequenceCode)) + @sequenceCode
            end
        end

  • sux0r mi pen3r (unregistered)

    rofl[pi]

  • (cs) in reply to ammoQ
    ammoQ:
    joost:
    ammoQ:
    Enric Naval:
    I use bash, but I can't pad strings with it, because I can't do a length() operation on a variable.
    ...
    ...$ length=$(echo $wtf|wc -c)
    ...

    <font size="4"><font size="2">$ echo ${#wtf}</font></font><font size="2">

    RTFM! LOL, LMAO
    </font>



    nice to know! guess you are right with that RTFM...


    you could also use:

    $ length=`expr length $wtf`

    but if the goal is to pad the number, why not just use

    $ printf "%.2d" $x

    $ help printf

    Cheers,
  • logic (unregistered)

    I'm a few years late on this, but how about this?:

    -- 1-Time Initialization! CREATE TABLE Letters (Item VARCHAR(25) NOT NULL) INSERT INTO Letters VALUES ('0') INSERT INTO Letters VALUES ('1') INSERT INTO Letters VALUES ('2') INSERT INTO Letters VALUES ('3') INSERT INTO Letters VALUES ('4') INSERT INTO Letters VALUES ('5') INSERT INTO Letters VALUES ('6') INSERT INTO Letters VALUES ('7') INSERT INTO Letters VALUES ('8') INSERT INTO Letters VALUES ('9') GO CREATE VIEW TwoLetters AS SELECT First.Item AS FirstLetter, Second.Item AS SecondLetter FROM Letters First INNER JOIN Letters Second ON First.Item + Second.Item <> '00' GO CREATE VIEW AddOneToTwoLetters AS SELECT FirstLetter AS OriginalFirstLetter, SecondLetter AS OriginalSecondLetter, FirstLetter, CAST(SecondLetter AS INT) + 1 AS SecondLetter FROM TwoLetters GO CREATE VIEW AddOneToTwoLetters2 AS SELECT OriginalFirstLetter, OriginalSecondLetter, FirstLetter, SecondLetter FROM AddOneToTwoLetters WHERE SecondLetter < 10 GO CREATE VIEW AddOneToTwoLetters3 AS SELECT OriginalFirstLetter, OriginalSecondLetter, CAST(FirstLetter AS INT) + 1 AS FirstLetter, 0 AS SecondLetter FROM AddOneToTwoLetters WHERE SecondLetter = 10 GO CREATE VIEW AddOneToTwoLetters4 AS SELECT OriginalFirstLetter, OriginalSecondLetter, FirstLetter, SecondLetter FROM AddOneToTwoLetters2 UNION ALL SELECT OriginalFirstLetter, OriginalSecondLetter, FirstLetter, SecondLetter FROM AddOneToTwoLetters3 GO CREATE VIEW AddOneToTwoLetters5 AS SELECT OriginalFirstLetter, OriginalSecondLetter, CAST(FirstLetter AS VARCHAR(2)) + CAST(SecondLetter AS VARCHAR(2)) AS Value FROM AddOneToTwoLetters4 GO CREATE VIEW AddOneToTwoLetters6 AS SELECT CAST(OriginalFirstLetter AS VARCHAR(1)) + CAST(OriginalSecondLetter AS VARCHAR(1)) AS FromValue, Value FROM AddOneToTwoLetters5 WHERE LEN(Value) < 3 UNION ALL SELECT '99', '00' -- Now run this to increment @sequenceCode SET NOCOUNT ON BEGIN TRY DROP TABLE #LookupTable END TRY BEGIN CATCH END CATCH SELECT * INTO #LookupTable FROM AddOneToTwoLetters6 ALTER TABLE #LookupTable ADD UNIQUE (FromValue) BEGIN TRY INSERT INTO #LookupTable VALUES (@sequenceCode, @sequenceCode) END TRY BEGIN CATCH -- This will never happen. END CATCH SET NOCOUNT OFF SELECT @sequenceCode = Value FROM #LookupTable WHERE FromValue = @sequenceCode DROP TABLE #LookupTable

    This code also handles the '00' case -- the original sequence of 'IF' statements didn't contain a case for it, so its value should remain unchanged. :-)

Leave a comment on “Fix The WTF”

Log In or post as a guest

Replying to comment #:

« Return to Article