Bring records from today's date up to 1 month ago SQLSERVER

0

How do I get records for today's date up to 1 month ago in SQLSERVER?

I know that for example, with 7 days, I use GETDATE () - 7. But for 1 month, what would it look like?

    
asked by anonymous 07.11.2018 / 14:21

2 answers

1

You can use DATEADD , this query will return the date in the last month starting from the last midnight

SELECT DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, DAY(GETDATE())-1)
    
07.11.2018 / 14:45
0
  

How do I get the records of today's date up to 1 month ago in SQLSERVER?

I suggest that you use the DATEDIFF function.

-- código #1
declare @UmMêsAtrás date;
set @UmMêsAtrás= datediff (month, -1, cast (current_timestamp as date));

--
SELECT ...
  from ...
  where coluna_data >= @UmMêsAtrás;

One must be careful to avoid the implicit conversion in the WHERE clause; thus, the @AnyMore variable must be declared in the same data type as coluna_data . Details on the article The dangers of implicit conversion

    
07.11.2018 / 14:38