今天选择mysql查询日期

时间:2016-02-11 13:55:29

标签: mysql date datetime sqlyog

我有一个数据库列 Date_Time with datatype varchar ,它存储格式为 dd / MM / yyyy h tt 的日期和时间。

例如,当我在datetimepicker上选择一个日期并设置一个时间时,它将在数据库2/11/2016 4 AM中显示。

现在我必须在Date_Time列中过滤或选择具有今天日期的行。但是,我的查询错误语法无效

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

1 个答案:

答案 0 :(得分:1)

我认为将日期时间值存储在字符串列中是一个非常糟糕的主意,但是......

你有

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

考虑了完整的日期时间,但您希望它只考虑日期部分,即&#dd / MM / yyyy h tt'中的前10个字符。所以只比较前10个字符:

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