I don't have to remember --- postgresql does that:

select to_char( generate_series( date( cast( to_char(current_date, 'YYYY' ) as integer) || +'-02-01' ) , date( cast( to_char(current_date, 'YYYY' ) as integer) + 1 || +'-01-01' ) , interval '1 month' ) - interval '1 day', 'DD') ; to_char --------- 31 28 31 30 31 30 31 31 30 31 30 31 (12 rows)

Or, golfed down a bit...

select to_char(generate_series('20140201','20150101',interval'1mon')-i +nterval'1','DD');

...it's shorter than the italian nursery rhyme! ;-)

update 1, yeah, I'm updating & golfing it a bit, sorry :)

update 2, (aargh! Oracle wins the Golf contest by 2 characters...

update 3, (aha! Oracle wins the Golf contest by only 1 character, (pg accepts '1mon' for '1 mon')

update 4, (aha!)

select to_char(generate_series('140201','150101',interval'1mon')-inter +val'1','DD'); -- Pg (9.5dev) SELECT TO_CHAR(ADD_MONTHS(DATE'14-1-1',LEVEL)-1,'DD')FROM Dual CONNECT + BY LEVEL<13; -- Oracle (10g) SELECT TO_CHAR(TO_DATE(LEVEL,'MM')-1,'DD')FROM Dual CONNECT BY LEVEL<1 +3; select to_char(to_date(generate_series(2,13)::text,'MM')+interval'1y - +1s','DD'); -- Pg. awful but works :) SELECT TO_CHAR(LAST_DAY(TO_DATE(LEVEL,'MM')),'DD')FROM Dual CONNECT BY + LEVEL<13; -- Oracle. "saved 3 char' SELECT TO_CHAR(TO_DATE(LEVEL,'MM')-1,'DD')FROM Dual CONNECT BY LEVEL<1 +3; -- Oracle. "start from december" (shortest) select 30+(3+(3*m+3)%5-(3*m+1)%5)/5from generate_series(3,13)f(m); -- + wrog's (short but incomplete: march through january)

Looks like Oracle wins this little golfing contest. Ah well... it has to have something to justify the price ;-))

Well played chacham++ :-)


In reply to Re: How do you remember the number of days in each month? by erix
in thread How do you remember the number of days in each month? by chacham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.