Help with query on sql server

2

I have the following question, I need to filter a query on sql server by date range and time range, for this I made the following query:

SELECT * FROM AHUMA00016 WHERE 
Fecha BETWEEN '28/5/18' AND '29/5/18' AND Hora BETWEEN '09:13' AND '09:28';

but this returns me in the range of dates, dates different from those specified in the range, it occurred to me that I could do a query by time range over the specified date range, I do not know how to do this very well to see if suddenly someone could help me with it, thank you very much!

Postscript: The idea would then be to implement the query in Laravel, but first I would like to clarify how to make the query correctly.

Structure of the table:

Some of the results that it throws:

    
asked by Sebastian Rodriguez 25.08.2018 в 19:11
source

1 answer

4

What happens is that the way you have your Query will bring you what is within the range of dates regardless of the time and what is within the range of hours regardless of the dates, you must group the conditions as follows:

This is when your fields are Date or Datetime, which is recommended

SELECT * FROM AHUMA00016 WHERE 
(Fecha BETWEEN '28/5/18' AND '29/5/18') AND (Hora BETWEEN '09:13' AND '09:28');

Given your situation it occurs to me that you create a view with the fields that you occupy of that table and the fields of date and time you apply a cast and then apply your select on that view, it would be something like this:

CREATE VIEW VistaFecha AS 
SELECT CAST(Fecha as DATE) 'FechaDate', CAST(Hora AS Time) 'HoraTime'
FROM AHUMA00016

Afterwards now, if you do the normal query, the fields will be Date and Time respectively.

    
answered by 25.08.2018 / 19:15
source