Get Mearest Time Using Date_Format MySQL - 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.

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

How do I filter a date row which greater than user-input and lower than user-input

I have journals_data table, which contains :
ID | Name | Input_date
001 | Example | 02/06/2016
002 | Example2 | 15/06/2016
003 | Example3 | 02/06/2016
And i have a vb.net form to indicates user input.
Select Period
Month : Year :
From : <cboMonth> <cboYear>
To : <cboMonth> <cboYear>
<OKButton> <CancelButton>
<cboMonth> items contains month name from January to Desember as string.
<cboYear> items contains year from the Input_date column in journals_data.
I want to select all rows from journals_data where its Month is greater than the "From-Month" value in the form, and lower than the "To-Month" value, also for year.
I got problem in the sql code, where the user input value for month is string (like January) not 01.
I have used DateTime.ParseExact() and ToString("00") technique to convert the January to 01 and combine it with the <cboYear> value and adding the 01 and 31 as the day value. But it won't work, it shows all the rows in the journals_data without filtering the Input_date.
Is there another way I can use to fix my problem?
I'm trying to find out what to do, and I got this and it works!
' To indicates the first day of the month
Dim dayFrom As String = "01"
' To indicates the last day of its month I count the days of its month
Dim dayTo As String
dayTo = DateTime.DaysInMonth(CInt(cboYear.Text), Month(CDate(cbomonthFrom.Text + cboYear.Text)).ToString("00"))
Connect()
DA = New OleDbDataAdapter("select * from journals_data where Input_date >= #" & _
Format(DateTime.ParseExact(dayFrom + " " + cbomonthFrom.Text + " " + cboYear.Text, "dd MMMM yyyy", CultureInfo.InvariantCulture), "dd/MM/yyyy") & "# and Input_date <= #" & _
Format(DateTime.ParseExact(dayTo + " " + cbomonthTo.Text + " " + cboYear.Text, "dd MMMM yyyy", CultureInfo.InvariantCulture), "dd/MM/yyyy") & "#", Conn)
DS = New DataSet
DS.Clear()
DA.Fill(DS, "journals")
dgv.DataSource = DS.Tables("journals")
Conn.Close()
I'm using MS-Access database.
Thanks to all your tips guys.

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.

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

calculate how many weekend days inside date?

I need to calculate how many weekend days inside 2 dates? what I mean is that I have 2 dates and want to know the count of Saturdays & Sundays between these dates.
I have the 2 dates on each record (from date - to date) and want to query the count of weekends.
The following VBA function will allow you to run Access queries of the form
SELECT CountWeekendDays([from date], [to date]) AS WeekendDays FROM YourTable
Just create a new Module in Access and paste the following code into it:
Public Function CountWeekendDays(Date1 As Date, Date2 As Date) As Long
Dim StartDate As Date, EndDate As Date, _
WeekendDays As Long, i As Long
If Date1 > Date2 Then
StartDate = Date2
EndDate = Date1
Else
StartDate = Date1
EndDate = Date2
End If
WeekendDays = 0
For i = 0 To DateDiff("d", StartDate, EndDate)
Select Case Weekday(DateAdd("d", i, StartDate))
Case 1, 7
WeekendDays = WeekendDays + 1
End Select
Next
CountWeekendDays = WeekendDays
End Function