Return list through a specific date

Asked

Viewed 50 times

4

Following a project I’m developing, a situation occurred to me where I can’t solve.

The purpose of this question is to know how to obtain on a given day the number of requests that a school library makes.
So far so good, this problem is solved.

Now, I came up with a new idea but it is difficult to solve and it is the following: I still wanted to do what I said above, but through the datetimepicker choose the day and appear through a database search to know if the requests of that day.

Below I will leave prints of the code I have at this time and the respective formats.

This is the select used for now:

SELECT Requisição.codRequisicao,
       Requisição.dataRequisicao,
       Utilizadores.numProcesso,
       Utilizadores.nomeUtilizador,
       Turma.Ano,
       Turma.Turma,
       Turma.Curso,
       TpUtilizador.Descricao,
       TipoServiço.descricaoTpServico
FROM Requisição
INNER JOIN TipoServiço ON Requisição.codTpServico = TipoServiço.codTpServico
INNER JOIN Utilizadores ON Requisição.numProcesso = Utilizadores.numProcesso
INNER JOIN Turma ON Utilizadores.codTurma = Turma.codTurma
INNER JOIN TpUtilizador ON Utilizadores.CodUtilizador = TpUtilizador.CodUtilizador
WHERE (Requisição.dataRequisicao >= CONVERT(date, GETDATE()))

For example on the day 26/06/2015 certain person made a request:

inserir a descrição da imagem aqui

This is where the requisitions are registered:

inserir a descrição da imagem aqui

  • I saw your edition in my reply, your mistake is in the first parentheses before the CONVERT remove it in such a way that it matches my answer

1 answer

1


In his SELECT there’s going to be a problem which is the >= when you go looking for dates prior to today, for example if you want yesterday will appear yesterday and today as it is greater or equal.

To solve this >=, just compare equal eliminating the hours, example:

WHERE CONVERT(date, Requisição.dataRequisicao) = CONVERT(date, GETDATE()))

Staying

SELECT Requisição.codRequisicao,
       Requisição.dataRequisicao,
       Utilizadores.numProcesso,
       Utilizadores.nomeUtilizador,
       Turma.Ano,
       Turma.Turma,
       Turma.Curso,
       TpUtilizador.Descricao,
       TipoServiço.descricaoTpServico
FROM Requisição
INNER JOIN TipoServiço ON Requisição.codTpServico = TipoServiço.codTpServico
INNER JOIN Utilizadores ON Requisição.numProcesso = Utilizadores.numProcesso
INNER JOIN Turma ON Utilizadores.codTurma = Turma.codTurma
INNER JOIN TpUtilizador ON Utilizadores.CodUtilizador = TpUtilizador.CodUtilizador
WHERE CONVERT(date, Requisição.dataRequisicao) = CONVERT(date, GETDATE()))

And in your case with the DateTimePicker, I believe you can do something like this:

Dim strSQL As String = "SELECT... " + 
                       "WHERE CONVERT(date, Requisição.dataRequisicao) = @dataEscolhida "
Using con = new SqlConnection("datasource...")
    con.Open()
    Using cmd = new SqlCommand(strSQL, con)
        cmd.Parameters.AddWithValue("@dataEscolhida", DateTimePicker1.Value)

        ' executeReader
    End Using
End Using
  • "Operand type Clash: date is incompatible with int" gives me this error

  • tries to put DateTimePicker1.Value without . Tostring(), I never did this test with dates in the query to find out which way

  • You’re using SqlCommand right?

  • Exactly. Continues to give syntax error.

  • @Forrobodo Take a look at the updated response

  • what is @dataEscolhida?

  • In strSQL is a parameter, then in strSQL cmd.Parameters.AddWithValue you add value to this parameter, why?

  • I don’t understand sorry

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.