Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265424
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:37:19+00:00 2026-05-21T06:37:19+00:00

Recently, I have been getting familiar with PostgreSQL(using 8.2) and found the date_trunc function

  • 0

Recently, I have been getting familiar with PostgreSQL(using 8.2) and found the date_trunc function extremely useful for easily matching time stamps between certain days/months/etc.
The real usefulness of the function, I believe, comes from the fact that it keeps the output in the format of a timestamp.

I have had to switch to mySQL(5.0) and find some of the date functions rather lacking in comparison. The extract function seems useful and the date function I have found solves some of my problems, but is there any way to replicate PostgreSQL’s date_trunc?

Following is an example of how I used to use date_trunc to match queried timestamps to only the last 4 months including the current month, but only if a week has passed into this month already:

WHERE date_trunc('month', QUERY_DATE) BETWEEN 
    date_trunc('month', now()) - INTERVAL '4 MONTH' AND 
    date_trunc('month', now() - INTERVAL '1 WEEK')

I have no idea how to recreate such a stipulation in mySQL. So, my question at the end of the day, is whether this type of query can be accomplished in mySQL by trying replicate date_trunc(and how) or whether I need to start looking at these types of queries in a different way to make them work in mySQL(and suggestions on how to do that)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:37:20+00:00Added an answer on May 21, 2026 at 6:37 am

    The extract function seems useful and the date function I have found solves some of my problems, but is there any way to replicate PostgreSQL’s date_trunc?

    Indeed, EXTRACT looks like it’s going to be the closest match for this specific case.

    Your original code in PG:

    WHERE date_trunc('month', QUERY_DATE) BETWEEN 
        date_trunc('month', now()) - INTERVAL '4 MONTH' AND 
        date_trunc('month', now() - INTERVAL '1 WEEK')
    

    Using EXTRACT:

    WHERE EXTRACT(YEAR_MONTH FROM QUERY_DATE)
          BETWEEN
              EXTRACT(YEAR_MONTH FROM NOW() - INTERVAL 4 MONTH)
          AND
              EXTRACT(YEAR_MONTH FROM NOW() - INTERVAL 1 WEEK)
    

    While it should be functionally identical, this is actually mangling the dates into a YYYYMM string before doing the comparison.

    Another option would be using DATE_FORMAT to rebuild the date string and force it to the beginning of the month:

    WHERE DATE_FORMAT(QUERY_DATE, '%Y-%m-01')
          BETWEEN
              DATE_FORMAT(NOW() - INTERVAL 4 MONTH, '%Y-%m-01')
          AND
              DATE_FORMAT(NOW() - INTERVAL 1 WEEK, '%Y-%m-01')
    

    Also, be aware that MySQL is really poor at dealing with date ranges, even when the field is indexed. You’re probably going to end up with a full table scan if you aren’t careful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I use an sql server regularly and have recently been getting frustrated by the
I have recently been working with Python using Komodo Edit and other simpler editors
I'm running Visual Studio 2005 Pro, and have been getting the following error recently:
I have recently been using cookie store and I want to transition to active
I recently have been getting more into C++. I have done some (very minimal)
We have been getting this Oracle connection pool exception a lot recently for our
Recently I have been investigating the possibilities of caching in ASP.NET. I rolled my
Recently I have been studying recursion; how to write it, analyze it, etc. I
Recently I have started playing with jQuery, and have been following a couple of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.