SQL Date Query Generator

Choose a start date, an end date, or both. The tool builds the date-range part of a SQL query. For DATETIME columns, use a half-open upper boundary when the input represents whole calendar dates.

Select one or both dates, then choose Generate.

Generated query pattern

SELECT *
FROM table_name
WHERE dt >= '2026-09-01'
  AND dt < '2026-09-11';

If the user selects September 10 as the final calendar date and dt is a DATETIME column, using the next day as an exclusive upper boundary includes every time on September 10. See date-range queries for the reasoning.

Backend PHP with PDO

For real database searches, do not concatenate submitted dates into SQL. Validate the values and bind them as parameters.

<?php
$dt1 = $_POST['dt1'] ?? '';
$dt2 = $_POST['dt2'] ?? '';
$conditions = [];
$params = [];

if ($dt1 !== '') {
    $from = DateTimeImmutable::createFromFormat('!Y-m-d', $dt1);
    if ($from === false || $from->format('Y-m-d') !== $dt1) {
        throw new InvalidArgumentException('Invalid start date.');
    }
    $conditions[] = 'dt >= :from_date';
    $params[':from_date'] = $dt1;
}

if ($dt2 !== '') {
    $to = DateTimeImmutable::createFromFormat('!Y-m-d', $dt2);
    if ($to === false || $to->format('Y-m-d') !== $dt2) {
        throw new InvalidArgumentException('Invalid end date.');
    }
    $nextDay = $to->modify('+1 day')->format('Y-m-d');
    $conditions[] = 'dt < :to_date';
    $params[':to_date'] = $nextDay;
}

$sql = 'SELECT * FROM table_name';
if ($conditions) {
    $sql .= ' WHERE ' . implode(' AND ', $conditions);
}

$stmt = $dbo->prepare($sql);
$stmt->execute($params);
?>



Subscribe to our YouTube Channel here



plus2net.com
saravanan

27-06-2014

if compare two dates that particular values is changed on rows and column by using stored procedure and displayed on gridview using C# with example and explaination
DR NIKHIL KUMAR GHORAI

14-02-2017

how to execute the statement and show data in a table injsp




SQL Video Tutorials










✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer