In a previous question, I needed to do a select by a specific date:
How to do a select by date in C #?
Now I need to make a select that brings records whose date is within a specific week or month. How should I proceed?
In a previous question, I needed to do a select by a specific date:
How to do a select by date in C #?
Now I need to make a select that brings records whose date is within a specific week or month. How should I proceed?
You need the SQL Server DATEPART function.
This function works like this: you enter the part of the date you want to search for, and the date itself. The function returns the number that corresponds to the part of the date.
For example:
select DATEPART(week, '2017-06-04')
This code above returns 14, because the week on which the date 04/06/2017 is found is the fourteenth week of the year.
So, to search for all the records that are found, for example, in the twentieth week of 2017, the search 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