Syntax for Last day of last Quarter

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • americanmc
    New Member
    • Nov 2012
    • 6

    Syntax for Last day of last Quarter

    Hi,
    I have a need to create a syntax , so that basing on the current_date, the sql generated will always give me the Last day of the Last Quarter.
    I am new to SQL, so need help
    thanks.
    Ed
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Define quarter. Calendar quarter? Fiscal Quarter? If fiscal, what is the start and end? It varies.

    But the basic algorithm is this:
    Code:
    if (month of some date) <= (month of last date in fiscal year) then
       use current year
    else
       use next year
    end if

    Comment

    • americanmc
      New Member
      • Nov 2012
      • 6

      #3
      We need the calendar quarter. So if I run the SQL now, it should give me sep 30 2012. and if I run it in Jan 2013, then it should give me 31 Dec 2012.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Use the date_trunc() function to get the first day of the quarter and then subtract one day.

        Comment

        • americanmc
          New Member
          • Nov 2012
          • 6

          #5
          the date_trunc() function does not work in Postgres. Can u send me the correct SQL for last day of Quarter.

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Sure it does. Here is PostgreSQL's list of date/time functions. You will see that date_trunc is on there.

            Comment

            • americanmc
              New Member
              • Nov 2012
              • 6

              #7
              yes, it does, i apprecaite. I am getting first day of curretn quarter, still looking for how to minus one day:
              select date_trunc('qua rter',current_d ate)
              Last edited by americanmc; Nov 29 '12, 10:15 PM. Reason: changes

              Comment

              • americanmc
                New Member
                • Nov 2012
                • 6

                #8
                Once again I apprecaite the help this forum is providing.

                Comment

                • Rabbit
                  Recognized Expert MVP
                  • Jan 2007
                  • 12517

                  #9
                  In that same link, it also shows you how to subtract a time interval.

                  Comment

                  • americanmc
                    New Member
                    • Nov 2012
                    • 6

                    #10
                    Yes, Thanks Rabbit, it works..

                    select date(date_trunc ('quarter',curr ent_date))-integer '1'

                    Comment

                    Working...