SQL Server - Dates, Greater than and Less than

Moshik

I have this stored procedure:

create procedure [dbo].[GetCarsByDates]
       (@startDate date, @returnDate date) 
as
    SELECT
        ManufacturerName, ModelName,
        CreationYear,
        Gear, CurrentKM,
        Picture,
        DATEDIFF(D, @startDate, @returnDate) * PricePerDay AS [Totalprice],
        PricePerDay, PricePerDayDelayed,
        InventoryCars.LicensePlate
    FROM 
        Models
    JOIN 
        Manufacturers ON Models.ManufacturerID = Manufacturers.ManufacturerID
    JOIN 
        InventoryCars ON InventoryCars.ModelID = Models.ModelID
    JOIN 
        CarsForRent ON CarsForRent.LicensePlate = InventoryCars.LicensePlate
    WHERE 
        CarsForRent.RentalStartDate < @startDate
        AND CarsForRent.RentalReturnDate < @returnDate
        AND CarsForRent.RentalReturnDate < @startDate
    ORDER BY 
        ManufacturerName, ModelName

I want to be able to select the properties by start and return dates. User inputs start date must be greater than return date and that is exactly what I did, but it still not working properly.

The problem is that I get rows results of items that are not available.

What is the problem with my where clause?

DhruvJoshi

I feel your query should be written like below. I assumed that you need to query all cars available from startDate to returnDate and need to check based on CarsForRent table's columns CarsForRent.RentalStartDate and CarsForRent.RentalReturnDate

create procedure [dbo].[GetCarsByDates]@startDate date, @returnDate date
 as
BEGIN
    select DISTINCT ManufacturerName, ModelName, 
           CreationYear,Gear, CurrentKM, 
            Picture,
            DATEDIFF(D, @startDate, @returnDate)*PricePerDay as[Totalprice],
            PricePerDay,PricePerDayDelayed, InventoryCars.LicensePlate
     from Models 
         join Manufacturers  on Models.ManufacturerID=Manufacturers.ManufacturerID
         join InventoryCars  on InventoryCars.ModelID=Models.ModelID
         join CarsForRent    on CarsForRent.LicensePlate=InventoryCars.LicensePlate

 where 
   @startDate > CarsForRent.RentalReturnDate AND 
   CarsForRent.RentalReturnDate >CarsForRent.RentalStartDate 
   AND @startDate<=@returnDate
     order by ManufacturerName, ModelName  
 END

if you do not need to check that returndate is >startdate remove this line from where clause :

AND @startDate<=@returnDate

I've created a sample fiddle Please add values to it in left side and play with the use cases http://sqlfiddle.com/#!6/00236/1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Greater Than and Less Than Equal to in SQL Server inner join NEED

From Dev

greater than and less than equals to in sql?

From Dev

greater than and less than equals to in sql?

From Dev

How to filter SQL by time (greater and less than)?

From Dev

Greater than or Less than equal to

From Dev

if greater than or less than and zero

From Dev

Having greater than or less than in hibernate named sql query

From Dev

jqueryui datepicker, compare two dates and adjust if greater or less than

From Java

How do I query for all dates greater than a certain date in SQL Server?

From Dev

If value is greater than, less than and becomes greater than again

From Dev

If value is greater than, less than and becomes greater than again

From Dev

Python Less/Greater than issue

From Dev

Creating a greater than but less than function in XML

From Dev

Strange Greater than or Less than Results

From Dev

Greater than, Less than string Lua (Lapis)

From Dev

Using '/' as greater than less than in Python?

From Dev

Javascript diamond? (Less than followed by greater than)

From Dev

Is there a greater than but less than function in python?

From Dev

Greater than and less than symbol in regular expressions

From Dev

AppleScript less than number or greater than number

From Dev

Regex that detects greater than ">" and less than "<" in a string

From Dev

Returning greater than and less than values in pandas

From Dev

Greater than and Less than Value SSRS 2008

From Dev

Greater than and less than symbol in regular expressions

From Dev

Strange Greater than or Less than Results

From Dev

Python greater than and less than operands not working?

From Dev

Use R switch for less than or greater than?

From Dev

.NET Greater than/less than not working

From Dev

implementing greater than / less than operator

Related Related

  1. 1

    Greater Than and Less Than Equal to in SQL Server inner join NEED

  2. 2

    greater than and less than equals to in sql?

  3. 3

    greater than and less than equals to in sql?

  4. 4

    How to filter SQL by time (greater and less than)?

  5. 5

    Greater than or Less than equal to

  6. 6

    if greater than or less than and zero

  7. 7

    Having greater than or less than in hibernate named sql query

  8. 8

    jqueryui datepicker, compare two dates and adjust if greater or less than

  9. 9

    How do I query for all dates greater than a certain date in SQL Server?

  10. 10

    If value is greater than, less than and becomes greater than again

  11. 11

    If value is greater than, less than and becomes greater than again

  12. 12

    Python Less/Greater than issue

  13. 13

    Creating a greater than but less than function in XML

  14. 14

    Strange Greater than or Less than Results

  15. 15

    Greater than, Less than string Lua (Lapis)

  16. 16

    Using '/' as greater than less than in Python?

  17. 17

    Javascript diamond? (Less than followed by greater than)

  18. 18

    Is there a greater than but less than function in python?

  19. 19

    Greater than and less than symbol in regular expressions

  20. 20

    AppleScript less than number or greater than number

  21. 21

    Regex that detects greater than ">" and less than "<" in a string

  22. 22

    Returning greater than and less than values in pandas

  23. 23

    Greater than and Less than Value SSRS 2008

  24. 24

    Greater than and less than symbol in regular expressions

  25. 25

    Strange Greater than or Less than Results

  26. 26

    Python greater than and less than operands not working?

  27. 27

    Use R switch for less than or greater than?

  28. 28

    .NET Greater than/less than not working

  29. 29

    implementing greater than / less than operator

HotTag

Archive