Search code examples
mysqldatedatetimesqlyog

Select mysql query date today


I have a database column Date_Time with datatype varchar which stores date and time formatted as dd/MM/yyyy h tt.

For example, when i pick a date on datetimepicker, and set a time, it will be like this in the database 2/11/2016 4 AM.

Now i have to filter or select just a row which has today's date in column Date_Time. However, my query is wrong invalid syntax.

MySqlConn.Open()
        Dim ds As New DataSet
        Dim da As New MySqlDataAdapter("Select Firstname, Lastname, Date_Time, Phone_Number from schedule WHERE Date_Time='" & Date.Now & "'", MySqlConn)
        da.Fill(ds)
        Dim message As String
        Dim dt As DataTable = New DataTable
        dt = ds.Tables(0)

        For Each dtRow As DataRow In dt.Rows    
            message = String.Format("Good day {0} {1}. You are scheduled today {2}." & Environment.NewLine & "-MULI DENTAL CLINIC.", _
                                    dtRow(0), dtRow(1), dtRow(2), dtRow(3))

            'SENDING SMS TO PATIENTS
            txtPhoneNumber.Text = dtRow(3)
            With frmSchedule.SerialPort1
                .Write("at+cfun=1" & vbCrLf)
                .Write("at" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("at+cmgf=1" & vbCrLf)
                Threading.Thread.Sleep(1000)
                .Write("at+cmgs=" & Chr(34) & txtPhoneNumber.Text & Chr(34) & vbCrLf)
                .Write(message & Chr(26))
                Threading.Thread.Sleep(1000)
            End With
        Next
        MsgBox("All messages were sent to scheduled Patients for today.", MsgBoxStyle.Information, "Sent")
        MySqlConn.Close()

Solution

  • I consider it a very bad idea to store a datetime value in a string column, but well...

    You have

    MySqlDataAdapter("Select Firstname, Lastname, Date_Time, Phone_Number 
      from schedule WHERE Date_Time='" & Date.Now & "'", MySqlConn)
    

    which takes the full datetime into account, but you want it to only consider the date part, i.e. the first 10 characters in 'dd/MM/yyyy h tt'. So compare the first 10 characters only:

    MySqlDataAdapter("Select Firstname, Lastname, Date_Time, Phone_Number 
      from schedule WHERE left(Date_Time,10) = left('" & Date.Now & "',10)", MySqlConn)