Results 1 to 4 of 4

Thread: SQL Query for getting available locations within 2 dates in Access SQL

  1. #1

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    SQL Query for getting available locations within 2 dates in Access SQL

    I have a table with locations and the dates when they are occupied in Access mdb database.

    CREATE TABLE `locations` (
    `LocationID` int(11) NOT NULL,
    `Location` varchar(255) NOT NULL,
    `StartDate` date NOT NULL,
    `EndDate` date NULL DEFAULT NULL
    )


    INSERT INTO `locations` VALUES (1, 'Venue A', '2023-10-01', '2023-10-10');
    INSERT INTO `locations` VALUES (2, 'Venue B', '2023-10-15', '2023-10-20');
    INSERT INTO `locations` VALUES (3, 'Venue C', '2023-10-05', '2023-10-12');
    INSERT INTO `locations` VALUES (4, 'Venue D', '2023-10-08', NULL);
    INSERT INTO `locations` VALUES (5, 'Venue E', '2023-10-25', NULL);
    INSERT INTO `locations` VALUES (6, 'Venue F', '2023-10-21', '2023-10-25');
    INSERT INTO `locations` VALUES (7, 'Venue G', '2023-10-30', '2023-11-05');
    INSERT INTO `locations` VALUES (8, 'Venue H', '2023-11-01', '2023-11-10');
    INSERT INTO `locations` VALUES (9, 'Venue I', '2023-11-15', NULL);
    INSERT INTO `locations` VALUES (10, 'Venue J', '2023-12-01', '2023-12-05');
    INSERT INTO `locations` VALUES (11, 'Venue K', '2023-12-10', '2023-12-20');
    INSERT INTO `locations` VALUES (12, 'Venue L', '2023-12-15', NULL);

    What would be the query to get locations that are free between 2 dates. For example, from '2023-10-21' and '2023-11-20' the result would be:

    Location
    Venue A
    Venue B
    Venue C
    Venue J
    Venue K
    Venue L
    My projects:
    Virtual Forms
    VBA Telemetry

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: SQL Query for getting available locations within 2 dates in Access SQL

    Tested in SQLite
    Code:
    SELECT LocationID, Location FROM 
    (SELECT LocationID, Location, StartDate, EndDate,
    CASE WHEN StartDate>'2023-11-20' THEN True ELSE False
        END Or
    CASE WHEN Coalesce(EndDate, '2039-12-31')<'2023-10-21' THEN True ELSE False END As MyCheck
    FROM locations) 
    WHERE MyCheck=True
    Returns

    LocationID Location
    1 Venue A
    2 Venue B
    3 Venue C
    10 Venue J
    11 Venue K
    12 Venue L


    EndDate = NULL means "Open End"?
    It's the reason i used the Coalesce, moving it into a ridiculous future

    Looks like MySQL. which Version?
    There might be another Approach to skin that cat

    Instead of the hardcoded Dates 2023-10-21 and 2023-11-20 you can use parameters
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  3. #3

    Thread Starter
    Addicted Member Davor Geci's Avatar
    Join Date
    Sep 2009
    Posts
    224

    Re: SQL Query for getting available locations within 2 dates in Access SQL

    Hello Zvoni,
    thanks for your reply.
    I'm testing this in MariaDB 10.4.24 but I need it for a project that is using old Access mdb database.

    I try to run your query on MariaDB, it should work, but don't know why I'm getting error:
    "1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE MyCheck=True' at line 7"

    When I use only the internal part of the SQL:
    Code:
    SELECT LocationID, Location, StartDate, EndDate,
    CASE WHEN StartDate>'2023-11-20' THEN True ELSE False
        END Or
    CASE WHEN Coalesce(EndDate, '2039-12-31')<'2023-10-21' THEN True ELSE False END As MyCheck
    FROM locations
    I get the result:

    LocationID Location StartDate EndDate MyCheck
    1 Venue A 2023-10-01 2023-10-10 1
    2 Venue B 2023-10-15 2023-10-20 1
    3 Venue C 2023-10-05 2023-10-12 1
    4 Venue D 2023-10-08 0
    5 Venue E 2023-10-25 0
    6 Venue F 2023-10-21 2023-10-25 0
    7 Venue G 2023-10-30 2023-11-05 0
    8 Venue H 2023-11-01 2023-11-10 0
    9 Venue I 2023-11-15 0
    10 Venue J 2023-12-01 2023-12-05 1
    11 Venue K 2023-12-10 2023-12-20 1
    12 Venue L 2023-12-15 1
    My projects:
    Virtual Forms
    VBA Telemetry

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,261

    Re: SQL Query for getting available locations within 2 dates in Access SQL

    "1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE MyCheck=True' at line 7"
    Change to ".... WHERE MyCheck=1"

    True/False gets "translated" to 1/0 (Integer)
    Changed True/False to /1/0
    Code:
    SELECT LocationID, Location FROM 
    (SELECT LocationID, Location, StartDate, EndDate,
    CASE WHEN StartDate>'2023-11-20' THEN 1 ELSE 0
        END Or
    CASE WHEN Coalesce(EndDate, '2039-12-31')<'2023-10-21' THEN 1 ELSE 0 END As MyCheck
    FROM locations) 
    WHERE MyCheck=1
    EDIT: For Queries against an Access-mdb, you'll have to change the Coalesce to "...IIf(EndDate is Null, #2039-12-31#, EndDate)..."
    The same for the CASE WHEN

    Code:
    IIf(StartDate>#2023-11-20#, 1, 0) Or 
    IIf(IIf(EndDate is Null, #2039-12-31#, EndDate)<#2023-10-21#, 1, 0) As MyCheck
    Last edited by Zvoni; Oct 13th, 2023 at 01:29 AM.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width