Previous dates

1

I am reporting where I need to always pick up the customer registration date in a previous month. Example: I am in the month of December 2017, I want to pick up the customers that were registered from November 1st 1 to the last day of November; And when I have it in January 2018, I want to pick up the customers who were registered from December 1 to the last day.

    
asked by anonymous 27.12.2017 / 12:41

2 answers

-1

To select clients registered in the previous month your code will be close to the examples below:

Entity Framework

db.Usuarios.Where(x => x.DataCriacao.Month == DateTime.Now.AddMonths(-1).Month);

SQL Server (T-SQL)

SELECT 
    * 
FROM 
    Tabela 
WHERE 
    Canal = 'FILIAIS' 
    AND Email like '%@%' 
    AND DATEPART(mm, DataCadastro) = DATEPART(mm, DATEADD(mm, -1, GETDATE())
    AND DATEPART(aaaa, DataCadastro) = DATEPART(aaaa, DATEADD(mm, -1, GETDATE())
    
27.12.2017 / 12:49
0

If you do this by query, it will always pick up the start date and end date of the previous month.

Select Convert(DateTime, Convert(Varchar(6), DateAdd(Month, -1, GetDate()), 112) + '01', 103) -- data inicio 
Select DateAdd(Day, -1, DateAdd(Month, 1, Convert(DateTime, Convert(Varchar(6), DateAdd(Month, -1, GetDate()), 112) + '01', 103))) -- data fim
    
08.01.2018 / 18:52