How to select in c# using data base / Sql-Server?

Asked

Viewed 107 times

1

In a previous question, I needed to select a specific date:

How to select a date in c#?

Now I need to make a select that brings the records whose date is within a specific week or month. How should I proceed?

  • Good morning Antonio, the question has few details, clarify the question with pieces of code or detail better the question. you could post the view_sale structure so we can help.

1 answer

1

You need the function DATEPART of SQL Server.

This function works like this: you inform the part of the date by which you want to perform the search, and the date itself. The function returns the number that corresponds to the date part.

For example:

select DATEPART(week, '2017-06-04')

This code returns 14, because the week in which the date 06/04/2017 is the fourteenth week of the year.

Therefore, to search for all the records that are found, for example, in the twentieth week of 2017, the survey would look like:

select
    *
from
    SuaTabela
where
    DATEPART(week, CampoData) = 20
    and DATEPART(year, CampoData) = 2017

To search for all the records that are found, for example, in the month of December 2016, you could do something like:

select
    *
from
    SuaTabela
where
    DATEPART(month, CampoData) = 12
    and DATEPART(year, CampoData) = 2016

Browser other questions tagged

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