SQL: Selecting the time range between two column - mysql

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")

Related

How do I get the value of a Form into an SQL query?

I am trying to get an Access SQL query that does this (semi-pseudocode below)
UPDATE SignIn SET SignIn.Complete=True, CompletedBy=(Select [FirstName] & " " & [LastName] AS EmployeeName From UserList where POid = Forms!HiddenUserCheck!txtPOid), CompletedDateTime=Now()
So after the query would run, the data in the database would look like
Complete EmployeeName CompletedDateTime
True John Smith 3/23/2017 8:34:10 AM
THe update query doesn't work because of syntax and not sure how to fix it.
The exact error message is
Invalid Memo, OLE, or HyperLink Object in subquery '[FirstName] & " "
& [LastName]'.
The query could be throwing a fit because of the Double Exclamation marks. Instead of
Forms!HiddenUserCheck!txtPOid
Try
Forms!HiddenUserCheck.txtPOid
You also have an extra ) at the end of your WHERE Statment
OK, then your issue may be that the subquery may return more than one record:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
(Select First([FirstName] & " " & [LastName]) AS EmployeeName
From UserList
Where POid = Forms!HiddenUserCheck!txtPOid),
CompletedDateTime = Now()
If your name fields are Memo/LongText fields, that may be the source of the error. If so, try:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
(Select First(Left([FirstName], 255) & " " & Left([LastName], 255)) AS EmployeeName
From UserList
Where POid = Forms!HiddenUserCheck!txtPOid),
CompletedDateTime = Now()
Edit.
You may try using DLookup for the subquery:
UPDATE
SignIn
SET
SignIn.Complete=True,
CompletedBy =
DLookup("[FirstName] & " " & [LastName]", "UserList", "POid = " & Forms!HiddenUserCheck!txtPOid & ""),
CompletedDateTime = Now()

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.

Time Range Validation in MySQL VB.Net

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.

"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!