Public Function MySQLDateTimeVar(inDate As Date, inTime As String) As String
Return "'" & inDate.ToString(format:="yyyy'-'MM'-'dd") & " " & inTime & "'"
End Function
用法:
假设我有 DateTimePicker1和 DateTimePicker2,用户必须定义开始日期和结束日期。不管日期是否相同。我需要仅使用 DATE 查询 DATETIME 字段。我的查询字符串很容易像下面这样构建:
Dim QueryString As String = "Select * From SOMETABLE Where SOMEDATETIMEFIELD BETWEEN " & MySQLDateTimeVar(DateTimePicker1.Value,"00:00:00") & " AND " & MySQLDateTimeVar(DateTimePicker2.Value,"23:59:59")
该函数为查询中的 DATETIME 字段生成正确的 MySQL DATETIME 语法,并且查询正确地返回该 DATE (或 BEWEEN The DATES)上的所有记录。
or
You can use select TIME(time) from appointment_details for time only
if time column is on timestamp , you will get date value from that timestamp using this query