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 7990611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:04:15+00:00 2026-06-04T13:04:15+00:00

I have a existing MySQL database with a column called month_year. It has date/time

  • 0

I have a existing MySQL database with a column called month_year. It has date/time data in format like:- 1974 – 1982, 11/2001 – 2008, 03/2007 on, up to 1987 and 11/2001 – 02/2007.

My website user will select a date and I need to give him some options depending on whether that date meets the criteria in DB.

for example:

up to 1987 in DB means all dates upto 1987
11/2001 - 2008 means all dates between this
03/2007 on - means all dates after this.

I need a SQL query to fetch all the rows matching this criteria. I think I need some regex to achieve this but am not sure. Any help would be great!

Thanks in advance.

Added: I do not have the choice to change the database. User will select year 2011 & month 10(October). Now I have to give him options based on his input. I will have to fetch all the rows whose month_year column satisfies this.

Column looks like this:

month_year

1974 - 1982
1988 - 10/1999
1988 - 10/1999
11/1999 - 10/2001
11/2001 - 2008
11/2001 - 2008
  • 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-06-04T13:04:16+00:00Added an answer on June 4, 2026 at 1:04 pm

    Trying to do this comparison using regexp’s in not sensible. You will get a very complicated regular expression that neither you nor anyone else will understand the next time you look at the code.

    Instead, parse the strings into their components, and do the work on the components. This generates longer code, but code that can be more easily followed.

    What you have a “span” expression that you need to parse into the first and last dates. Let me give you the solution in SQL Server. I’m choosing this dialect simply because it is easier for me; I think you can readily adapt it to mysql (functions for dates and character strings seem to have different names in each database).

    The following query adds columns month1, year1, month2, and year2 to the data:

    select t.*,
           (case when charindex('/', part1) > 0 -- have month or not?
                 then cast(left(part1, charindex('/', part1)) as int)
                 else 1
            end) as Month1,
           (case when charindex('/', part1) > 0 -- have month or not?
                 then cast(substring(part1, charindex('/', part1), 100) as int)
                 else cast(part1 as int)
            end) as Year1,
           (case when charindex('/', part2) > 0 -- have month or not?
                 then cast(left(part2, charindex('/', part2)) as int)
                 else 1
            end) as Month2,
           (case when charindex('/', part2) > 0 -- have month or not?
                 then cast(substring(part2, charindex('/', part2), 100) as int)
                 else cast(part2 as int)
            end) as Year2
    from (select t.*,
                 left(t.span, charindex(t.span, '-')-2) as part1,
                 substring(t.span, charindex(t.span, '-')+2) as part2
          from t
         )
    

    You can now use these expressions to do the comparisons that you want. With one caveat: the mixing of years and years with months may give unexpected results. You need to be particular careful when a user specifies something like 9/2008 – 2010. Is this all months in 2010? Or just the first month?

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

Sidebar

Related Questions

I have an existing database in mysql. One of my tables has discontinuous ids.
I have a table called jobs in a MySQL database. One user adds data
I have a existing MySQL database which has large number of tables. But the
I have a MySQL database , with a column that is date type DATETIME
I'm working on a PHP-based webapp that has an existing MySQL database where all
I have an existing column in my SQL Server database. I have tried about
I have a pre-existing mysql database containing around 50 tables. Rather than hand code
I'd like to add a column to existing table in MySQL with name =
I have an existing MySQL database schema in production for an PHP5 application. The
I have an existing MySQL database on a Linux computer, and I need to

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.