ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

SQL return two-digit month

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • SQL return two-digit month

    To break down my question to a very simple SQL:

    Code:
    select char(year(current_date)), char(month(current_date))
    from sysibm.sysdummy1
    How can I modify this to always return a two-digit month? For example, in Feb I would like to see '02' instead of '2'. I was trying to play around with CAST to see if that would do it but I haven't got it right.

    Thanks in advance.

  • #2
    Here's one way:
    Code:
    select char(year(current_date)), substr(digits(month(current_date)),9,2)
    from sysibm.sysdummy1

    Comment


    • #3
      Here 2 other ways:
      Code:
      values(Right(digits(Month(Current_Date)), 2),
             Right(Digits(Extract(Month from Current_Date)), 2));
      Birgitta

      Comment


      • #4
        varchar_format(current timestamp, 'MM')

        returns '02' for February

        Ringer

        Comment


        • #5
          Thanks all for your solutions! I ended up using Ringer's as it was the shortest.

          Comment


          • #6
            varchar_format(current timestamp, 'MM')

            You can also use a date field - doesn't need to be a timestamp... in this case it's no big deal but if you do this with a date field you don't need to convert it to a timestamp first...

            varchar_format(current date, 'MM')

            Comment

            Working...
            X