SQL "Where" problem

Asked

Viewed 63 times

0

I am doing a query in SQL and as a constraint I want the query to return the data from the first day of the previous month until today.

For example: If I run today (10/04/2019) the query, will return only entered data from 01/03/2019 until now.

To do this I created the following restriction:

SD1010.D1_DTDIGIT>=(YEAR(GETDATE())+ (MONTH(GETDATE())-1) + ('01')) AND
SF1010.F1_DTDIGIT<=GETDATE()

With this constraint the query does not return any data.

  • Welcome, this forum is in Portuguese, please translate your question.

  • Welcome to the Stackoverflow in Portuguese. As the name suggests, the official language used here is Portuguese. So, could you please translate your question? If you prefer, you can also ask the same question on Stackoverflow website in English.

  • You didn’t report which one SGBD. And neither will your table structures.

  • 1

    I believe that the result of YEAR(GETDATE())+ (MONTH(GETDATE())-1) is numerical and not a string of characters as you seem to expect (in your example it would be 2022 and not "201903").

1 answer

0

Try it that way:

DECLARE @DATAINICIO DATETIME;    
SET @DATAINICIO = CONCAT(YEAR(GETDATE()), '-', MONTH(GETDATE()) - 1, '-', '01')
    SELECT * FROM TABELA WHERE DATA >= @DATAINICIO AND DATA <= GETDATE();

Browser other questions tagged

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