Ever heard of sprintf()?



  • A friend of mine was asking me questions about how to do certain SQL-y things in Rails, which I've been introducing him to. One of his "How would I do this in Rails?" SQL pastes includes the following fragment:

    CONCAT ((CASE WHEN number < 10 THEN '000' WHEN number < 100 THEN '00' WHEN number < 1000 THEN '0' END), number)


  • I assume he's trying to print a 4 digit number in SQL?  Much easier way:

     right('0000'+cast(number as varchar(4)), 4)



  • @Ken said:

    I assume he's trying to print a 4 digit number in SQL?  Much easier way:

     right('0000'+cast(number as varchar(4)), 4)

    I'm actually trying to get him to just use sprintf() after the results have been returned. It makes a lot more sense for how he's using the query's output.



  • @Wolftaur said:

    CONCAT ((CASE WHEN number < 10 THEN '000' WHEN number < 100 THEN '00' WHEN number < 1000 THEN '0' END), number)
     

    Bah, why not use:

     CONCAT (("0000" & number), number)

     Okay, okay, just joking.  It probably doesn't work in Ruby anyways.



  • @shepd said:

    @Wolftaur said:

    CONCAT ((CASE WHEN number < 10 THEN '000' WHEN number < 100 THEN '00' WHEN number < 1000 THEN '0' END), number)
     

    Bah, why not use:

     CONCAT (("0000" & number), number)

     Okay, okay, just joking.  It probably doesn't work in Ruby anyways.

    Well, Rails does allow you to construct your own SQL if you want to or need to, but... Ruby also has sprintf(). :)



  • @Wolftaur said:

    Well, Rails does allow you to construct your own SQL if you want to or need to, but... Ruby also has sprintf(). :)

    Much easier solution: "SELECT number + 10000 AS number ..." then just chop off the first character.

     

    Too bad Ruby doesn't have some kind of string formatting function that would make this easier.  This is just another reason for me to tell people to avoid Ruby like the plague, in addition to it not even being Turing-complete, seeing as it is interpreted.



  • @morbiuswilters said:

    Too bad Ruby doesn't have some kind of string formatting function that would make this easier.  This is just another reason for me to tell people to avoid Ruby like the plague, in addition to it not even being Turing-complete, seeing as it is interpreted.

    Actually, Ruby does indeed have string formatting functions, such as sprintf(), which works the same as Perl's or C's...



  • @Wolftaur said:

    @morbiuswilters said:

    Too bad Ruby doesn't have some kind of string formatting function that would make this easier.  This is just another reason for me to tell people to avoid Ruby like the plague, in addition to it not even being Turing-complete, seeing as it is interpreted.

    Actually, Ruby does indeed have string formatting functions, such as sprintf(), which works the same as Perl's or C's...

    I know Ruby has lots of wiz-bang cool stuff and it's very object-oriented, but you think a simple string formatting function would be built-in.  Just something like perl or C's sprintf()... 



  • @morbiuswilters said:

    I know Ruby has lots of wiz-bang cool stuff and it's very object-oriented, but you think a simple string formatting function would be built-in.  Just something like perl or C's sprintf()...

    Um...

    It is.

    $ irb  # Interactive Ruby
    >> sprintf("%04d", 5)
    => "0005"
    >> sprintf "%+5d", 3
    => "   +3"
    >> "%-5d" % 2
    => "2    "
    >>


  • @Wolftaur said:

    @morbiuswilters said:

    Too bad Ruby doesn't have some kind of string formatting function that would make this easier.  This is just another reason for me to tell people to avoid Ruby like the plague, in addition to it not even being Turing-complete, seeing as it is interpreted.

    Actually, Ruby does indeed have string formatting functions, such as sprintf(), which works the same as Perl's or C's...

     

    I know that you can't tell that he is joking because he used perfect capitalization and punctuation, but it should be clear that it's a joke from the fact that he said it isn't Turing complete because it is interpreted.



  • @tster said:

    I know that you can't tell that he is joking because he used perfect capitalization and punctuation, but it should be clear that it's a joke from the fact that he said it isn't Turing complete because it is interpreted.

    I ran out of coffee yesterday, so... I'm missing the should-be-obvious left and right today. :/



  • @Wolftaur said:

    Ruby does indeed have string formatting functions, such as sprintf(), which works the same as Perl's...

    Anything working the same as Perl is a clear sign of trouble.

     



  • @Wolftaur said:

    @tster said:

    I know that you can't tell that he is joking because he used perfect capitalization and punctuation, but it should be clear that it's a joke from the fact that he said it isn't Turing complete because it is interpreted.

    I ran out of coffee yesterday, so... I'm missing the should-be-obvious left and right today. :/

    It's okay, I wasn't making fun of you, but instead the first two commenters who kept suggesting odd SQL workarounds and ignoring your clear statement on sprintf(). 



  • @Soviut said:

    @Wolftaur said:

    Ruby does indeed have string formatting functions, such as sprintf(), which works the same as Perl's...

    Anything working the same as Perl is a clear sign of trouble.

    PCRE? 



  •  The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.



  • @spacecadet said:

    The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.

    What I want to know is when did "boot" become a euphemism for penis?  I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 



  • @morbiuswilters said:

    @spacecadet said:

    The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.

    What I want to know is when did "boot" become a euphemism for penis?  I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 

     

    Must be a brittish thing.  They're always fucking up the English language.



  • @amischiefr said:

    @morbiuswilters said:

    @spacecadet said:

    The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.

    What I want to know is when did "boot" become a euphemism for penis?  I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 

     

    Must be a brittish thing.  They're always fucking up the English language.

    Tell me about it.  What a bunch of boots...


  • @morbiuswilters said:

    @spacecadet said:

    The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.

    What I want to know is when did "boot" become a euphemism for penis?  I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 

     

    That reminds me, your dad says hi.



  • @spacecadet said:

     The last person I heard who suggested that you can write Rails without learning Ruby first is still trying to get my boot out of his ass.

    I'm not so much trying to teach him Rails as to illustrate how a lot of the stuff that drives him insane in what he uses now is much easier in Rails. I've told him I won't give him actual Rails lessons until he's read and understood the pickaxe book. :)



  • @morbiuswilters said:

    I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 

     

     We don't want to hear about penises slipping out of your grasp.



  • @bullestock said:

    @morbiuswilters said:

    I'm usually very well-educated on penises, but somehow this one slipped out of my grasp. 

    We don't want to hear about penises slipping out of your grasp.

    I know it's tragic, but we must not allow ourselves to forget, lest history repeat itself. 



  • Does ruby/rails have an sql library that will do string escaping for you? Here he's only using a number so I guess it's not necessary, but it probably doesn't hurt to get him in the habit before he starts building sql queries containing user-entered strings.



  • @Wolftaur said:

    Ruby also has sprintf(). :)

    It does? Damnit.



  • @SuperousOxide said:

    Does ruby/rails have an sql library that will do string escaping for you?
    Of course not.  Ruby isn't a real language, it's a scripting language.



  • @morbiuswilters said:

    @Wolftaur said:

    @tster said:

    I know that you can't tell that he is joking because he used perfect capitalization and punctuation, but it should be clear that it's a joke from the fact that he said it isn't Turing complete because it is interpreted.

    I ran out of coffee yesterday, so... I'm missing the should-be-obvious left and right today. :/

    It's okay, I wasn't making fun of you, but instead the first two commenters who kept suggesting odd SQL workarounds and ignoring your clear statement on sprintf(). 

    Those suggestions reminded me of those queries I keep getting sent:

    [code]SELECT some_id,some_other_field,convert(char(10),some_date_field,103) registry_date FROM wtf[/code]

    note that convert() function. I asked why they did it, and the answer was that they wanted to get the yyyyMMdd date format. It seems that they didn't even know about the DateFormat um... formatter:

    [code]DateFormat format = new DateFormat("yyyyMMdd");[/code]

    [code] java.sql.Date date = rs.getDate("some_date_field");[/code]

    [code]String formattedDate = format.format(date);[/code]

    would do the job, and you can also keep the actual date as a Date, instead of a string. Or even better: use a formatting output tag in the jsp itself. Note that these guys already used output-formatting tags for numbers, and failed to notice there's a similar one for dates.


Log in to reply