Time Range Validation in MySQL VB.Net - mysql

I have schedule_tbl in table in my Database for the professor and have starting_time, ending_time and day column. I would like to do is, if I inserted a starting of 7:00:00 to 10:00:00 then all that counters 7:00:00-10:00:00 is invalid.
Example my record
starting_time ending_time day
7:00:00 10:00:00 Monday
Inserting
6:00:00 8:00:00 Monday
Then it must be invalid. So far this is my code.
("SELECT * FROM schedule_tbl WHERE "' and starting_time >= '" & _
Me.dtpStart.Value.ToString("HH:mm:ss") & "' and ending_time <= '" & _
Me.dtpEnd.Value.ToString("HH:mm:ss") & "' and day = '" & ComboBox4.Text & "'", conn3)
Its working but only in the range between the starting_time and ending_time. If I input 6:00:00 for starting_time and 9:00:00 for ending_time, its adding up to the Database which should be invalid.

The correct logic for determining if two periods overlap is that the first starts before the second ends and the first ends after the second starts. The logic to get overlaps looks like this:
SELECT s.*
FROM schedule_tbl s
WHERE s.starting_time < $EndTime and
s.ending_time > $StartTime;
Note: You might want >= and <= for your problem rather than strict inequalities.

My bad there just a problem with my if else statement instead of using OR I coded AND its working fine now.

Related

Query with DLookup for date between two dates in the refererence table

I have a table that defines date ranges:
Reference_table:
[refID] , [start] , [end]
1 1/1/2020 3/3/2020
2 4/3/2020 7/6/2020
3 8/6/2020 10/10/2020
and another that has date column
Incident_table:
[IncidentID] , [incident_date]
56 1/2/2020
57 8/3/2020
58 12/5/2020
Now I'd like to define a Query, that gives me the reference_id to every Incident.
I tried to use
SELECT Reference_table.refID, Incident_table.IncidentID
FROM Reference_table
INNER JOIN Incident_table
ON (Reference_table.end >= Incident_table.incident_date >= Reference_table.begin);
which gave me an empty dataset
and with a lookup
SELECT
IncidentID,
DLookup("refID";"Reference_table";"[start]<= #" & Incident_date & "# <= [end]") as reference
FROM Incident_table
Which gave me all the records, but reference was #Error (instead of 1..3)
You can use a good ol' where-join:
SELECT
Reference_table.refID,
Incident_table.IncidentID
FROM
Reference_table,
Incident_table
WHERE
incident_date Between Reference_table.begin And Reference_table.end
Turns out, the DLookup was the right way, but different language versions of access throw you off the right path. For reference, a working solution is:
SELECT
IncidentID,
DLookup(
"refID",
"Reference_table",
"[start]<= #" &
Format([Incident_date],"mm\/dd\/yyyy") &
"# between [start] and [end]") &
"# <= [end]"
) as reference
FROM Incident_table

DCount not working on Access

I donĀ“t know whats wrong with this query... I want to know how many records are on another query named: Querie_Planilla that have the following characteristics:
The date on the field PeriodoInicial has to be greater or equal to the date [FechaInicio] but a year ago
The date on the field PeriodoFinal has to be smaller to the value [FechaInicio] (Both of this conditions consider the first day of the month in FechaInicio
It has to have the same ID im looking for: PlazaID = [Vacaciones]![PlazaID]
Here is the code:
DCount("PlazaID","Querie_Planilla","PeriodoInicial >= #" & DateSerial(DatePart("yyyy",[FechaInicio])-1,
DatePart("m",[FechaInicio]),1) & "# AND PeriodoFinal < #" & DateSerial(DatePart("yyyy",[FechaInicio]),
DatePart("m",[FechaInicio]),1) & "# AND PlazaID = '" & [Vacaciones]![PlazaID] & "'")
Right now its returning 0 but at least i have 3 for each ID

Get Mearest Time Using Date_Format MySQL

This is my code so far
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and ending_time >= starting_time
and day = DATE_FORMAT(Now(),'%W')"
starting_time ending_time day
07:00:00 08:30:00 Tuesday
08:30:00 10:00:00 Tuesday
It only returns the first record in the database. What I want is if the time now is 8:30AM then the next schedule will be show up is 8:30AM - 10:00AM. How can I do this with using my codes above? Or Is there any other way to make this happen?
Sample Schema
You want to add the "I want the starting time to be less-than-or-equal to now" logic which can be accomplished by starting_time<=DATE_FORMAT(NOW(),'%T'):
"select * from schedule_tbl where sname = '" & Label4.Text &"'
and starting_time <= ending_time and starting_time<=DATE_FORMAT(NOW(),'%T')
and day = DATE_FORMAT(Now(),'%W')"
Note that, as mentioned in comments, ending_time >= starting_time is redundant because it's satisfied by starting_time <= ending_time.

SQL: Selecting the time range between two column

i am working on a part of my system called Patient Record Management system and in it there is an Appointment Management, and in Appointment you make a timeslot unavailable to others once occupied so here lies the problem:
there are 3 columns that are in my database: date(Date), TimeIn(Time), TimeOut(Time)
this is what i've done so far:
sqlQuery = "SELECT * FROM appointment where date = '" & datePicker.Value.ToString("dd-MM-yyyy") & "' and TimeIn <= CAST('" & timeinPicker.value.ToString("HH:mm") & "' AS Time) and TimeOut >= CAST('" & timeoutPicker.value.ToString("HH:mm") & "' AS Time)"
example if i put 12:00 to timeinPicker and 13:00 to timeoutPicker, all time between 12:00 and 13:00 should be selected, but my problem is it won't get selected, it can only select it if i input exactly 12:00 and 13:00 but when i put 12:01 and 12:59, the sql cant select it like it didnt exist
is there some way to select them so i can know which time is occupied.
P.S. i'm using MySql
if you are using java means replace & character with + and try
sqlQuery = "SELECT * FROM appointment where date = '" + datePicker.Value.ToString("dd-MM-yyyy") + "' and TimeIn <= CAST('" + timeinPicker.value.ToString("HH:mm") + "' AS Timein) and TimeOut >= CAST('" + timeoutPicker.value.ToString("HH:mm") + "' AS Timeout)";
First thing is you should use the parameterized queries always that give you protection from the Sql Injection.
You can change your query like this,
string query = "SELECT * FROM appointment where date = #DateValue and TimeIn <= CAST(#TimeInValue AS Time) and TimeOut >= CAST(#TimeOutValue AS Time)"
And then set the Paramters values to following values using the AddParamter() I don't know which programming language you are using so there will be similar method to add the parameter use that,
#DateValue = datePicker.Value.ToString("dd-MM-yyyy")
#TimeOutValue = timeoutPicker.value.ToString("HH:mm")
#TimeInValue = timeInPicker.value.ToString("HH:mm")

"Data type mismatch in criteria expression"

I have tried to select all records from a table from date1 to date2, Example Jun 28, 2014 to Jan 05, 2015, for display. Basically, sort out selected records based on date criteria. Big thanks if anyone could point out my mistake.
What I tried to do here is allowed user to select specific dates from calendar to view the records
I have gone through all answers, but still couldn't find similar solution to mine. I think, there may be some error in my syntax.
sqlDateRangeSearch = "Select * from BatteryDataTable where ((BatteryDateChanged) <= ""*" & Me.FromDTPicker.Value & "*"")" & " and ((BatteryDateChanged) <= " & """*" & Me.ToDTPicker.Value & "*""));"
Me.RecordSource = sqlDateRangeSearch
I noticed you have wildcard characters in the search criteria for the query. If using dates, you'll want to avoid those. Also, you don't need quotes for dates in access queries. If you're dynamically creating SQL I'd use something like:
sqlDateRangeSearch = "Select * from BatteryDataTable where (BatteryDateChanged <= #" & Me.FromDTPicker.Value & "#)" & " and (BatteryDateChanged <= #" & Me.ToDTPicker.Value & "#));"
Just a side note, your comparison operators are the same. I think you want it to say something like (remember the "#" symbols):
...WHERE (Field1 >= #Date1# AND Field1 < #Date2#);
Hope that helps!