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

  • Home
  • SEARCH
  • 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 7644849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:47:19+00:00 2026-05-31T09:47:19+00:00

I am trying to select rows which are within a certain time period: $sql

  • 0

I am trying to select rows which are within a certain time period:

$sql = mysql_query(" SELECT * FROM table WHERE ... AND 
       date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
       OR date ISNULL");

The format of the dates saved in this column are mm/dd/yyyy

a. how do I select the time interval? currently it is selecting all the rows, not just those within this date range.

b. Am I using the corret syntax to make sure we include all null options as well?

Update.

Following the answers and comments I changed the column type to “Date” (it was varchar), and have also changed the current type of the existing entries to the correct date format.

I now have 2 questions:
a. This is still not working… What else might be the problem?
b. I want to change in PHP where the entry is saved. How ahould I go about this? Is the following correct:

 $new_date = mysql_query("STR_TO_DATE($old_date_string,'%m/%d/%Y')");

Solution.

First, I changed the column type to DATE (from VARCHAR).

Second, I moved and converted all the existing entries from their existing column and type (str) to new type (date) and new column.

Third, I added parentheses to the “date” section.

Fourth, I changed ISNULL to IS NULL – addin a space, which changes it from a function to a statement.

Fifth, I am now updating my script so that in the future dates will be saved to the new column in the new type, converting them to the correct fprmat before saving them to the database.

Thankyou @nnicholas and everybody.

  • 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-31T09:47:20+00:00Added an answer on May 31, 2026 at 9:47 am

    If you are really storing your dates as strings I suggest you update your table with something like the following –

    -- add a new column of type DATE
    ALTER TABLE `table` ADD COLUMN `date_new` DATE AFTER `date`;
    -- populate the new column from the old one
    UPDATE `table` SET `date_new` = STR_TO_DATE(`date`,'%m/%d/%Y');
    -- drop the old column
    ALTER TABLE `table` DROP COLUMN `date`;
    -- rename the new column
    ALTER TABLE `table` CHANGE `date_new` `date` DATE;
    

    After making these changes handling dates will be much easier for you. When inserting the dates that you are receiving you simply use STR_TO_DATE('new_value','%m/%d/%Y') to convert the date during the insert –

    mysql_query("INSERT INTO `table` (field1, field2, `date`) 
                   VALUES('value1', 'value2', STR_TO_DATE('$old_date_string','%m/%d/%Y')");
    

    This assumes that $old_date_string has already been sanitised.

    With your table structure updated the standard date arithmetic functions will work as intended.

    $sql = mysql_query("SELECT *
                        FROM table
                        WHERE ...
                        AND (`date` BETWEEN ('$date' - INTERVAL 2 YEAR) AND ('$date' + INTERVAL 2 YEAR) OR `date` IS NULL)");
    

    $date must be in Y-m-d format.

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

Sidebar

Related Questions

I am trying to select rows from a table where one of the (NVARCHAR)
Well, I was trying to select rows from one table if there are no
I'm trying to select data from a table defined similar to the following :
I'm trying to select a random 10% sampling from a small table. I thought
I'm trying to select a column from a single table (no joins) and I
I'm trying to select more than 80,000 record in SQL Server in a table
I'm trying to select two distinct numbers id1 and id2 from the following table:
I am trying to get 2 rows from a table at one shot. The
I'm trying to select rows with field timestamp , which has a length shorter
Hi I am trying to find all rows in my database (SQL Server) which

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.