1
Talk, you guys! I have dates with two types of different formats in the database, example:
- MM/dd/yyyy
- dd/MM/yyyy HH:mm:ss
But when doing the query in VB.NET, I did so and it doesn’t work.
Public Function teste (ByVal dtpInicio As String, ByVal dtpFim As String) As List(Of Teste)
'Consulta com a data dd/MM
Dim retorno As New List(Of Teste)
Try
Conectar()
cmd.CommandText = "SELECT * FROM NOME_TABELA WHERE NOME_COLUNA BETWEEN '" &
dtpInicio.ToString("dd/MM/yyyy HH:mm:ss") & "' AND '" & dtpFim.ToString("dd/MM/yyyy HH:mm:ss") & "' ORDER BY NOME_COLUNA"
dr = cmd.ExecuteReader
While dr.Read
retorno.Add(New Teste With {
.NOME_COLUNA = dr(0)
})
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Desconectar()
End Try
'Consulta com a data dd/MM
Dim retorno As New List(Of Teste)
Try
Conectar()
cmd.CommandText = "SELECT * FROM NOME_TABELA WHERE NOME_COLUNA BETWEEN '" &
dtpInicio.ToString("MM/dd/yyyy") & "' AND '" & dtpFim.ToString("MM/dd/yyyy") & "' ORDER BY NOME_COLUNA"
dr = cmd.ExecuteReader
While dr.Read
retorno.Add(New Teste With {
.NOME_COLUNA = dr(0)
})
End While
Catch ex As Exception
MsgBox(ex.Message)
Finally
Desconectar()
End Try
Return retorno
End Function
Any tips, you guys?
Try the format Yearday, thus:
ToString("yyyyMMdd")
– Ricardo Pontual
Generally in database the date format is "YYYY-MM-DD", make a select in your database and please check.
– Wellington Araujo
@Wellingtonaraujo I did select on my base and appeared. But the problem is not SQL but VB.NET, because it appeared the message now like this: "Unable to cast Object of type 'System.String' to type 'System.Iformatprovider'"
– Joao Torres Moreira
@Did the Date column of your table actually create Datetime? From what I am seeing first you will have to solve the data of the Date column of your table, it has to have a format. Ex: "dd/mm/yyyy 00:00:00" or "yyyy/mm/dd 00:00:00". Checks whether the column type is Datetime itself.
– Wellington Araujo
From what I saw the table column was created as Short Text. NOTE: I am accessing the database through ACCESS.
– Joao Torres Moreira
Then @Joaotormoreira first you will have to fix the bank, otherwise you will be just covering hole, the way this table will accept anything in the field, understood... see the article of the link below... https://support.office.com/pt-br/article/formatr-o-field-date-e-time-no-access-47fbbdc1-52fa-416a-b8d5-ba24d881b698
– Wellington Araujo