Select between month and year only dates

Asked

Viewed 626 times

2

Good evening, I believe my doubt is easy for some, but I am beginner is breaking my head, I need to do a search with select between the two dates, searching only the month and the year... I did with the month and it worked, now only the year is missing. If you can help me I appreciate.

SELECT * FROM pagamento WHERE Month(data) between '06' AND '07';

It brings the right results between the months, now I need you to look for the year. From now on I thank anyone who can help me.

  • 1

    SELECT * FROM payment WHERE year(date) between '2014' AND '2018';

  • @Leocaracciolo with this will be returned the specified months and years and not "between dates".

  • It brings the right results between the months, now I need you to look for the year

  • And how are the dates in the bank? DATETIME, DATE ??

5 answers

3

Assuming the dates are 2010-09-15 and 2020-04-08, you could use DATE_FORMAT to replace the day of the initial date by 01 and the function LAST_DAY to replace the end date day with the biggest day of the month. So you would pick up the records within the past months, regardless of the day.

For example:

SELECT * 
FROM pagamento 
WHERE data between 
    DATE_FORMAT('2010-09-15' ,'%Y-%m-01')  -- ignora o dia e usa dia 01
    AND LAST_DAY('2020-04-08');            -- ignora o dia e usa o último do mês

2

The DATE_FORMAT() function formats a date as specified

Bank with date in DATETIME format

SELECT * FROM pagamento
where DATE_FORMAT(data,'%Y%m') >= DATE_FORMAT('2010-06-01 00:00:00','%Y%m') and DATE_FORMAT(data,'%Y%m') <= DATE_FORMAT('2020-07-01 00:00:00','%Y%m')

Bank with date in DATE format

SELECT * FROM pagamento
where DATE_FORMAT(data,'%Y%m') >= DATE_FORMAT('2010-06-01','%Y%m') and DATE_FORMAT(data,'%Y%m') <= DATE_FORMAT('2020-07-01','%Y%m')

In both cases you can put any day, and in the case of DATETIME any time the return will be the same, ie selects between the month/year. Must be valid dates, can not put for example day 32

Example with DATE type

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Regardless of the days informed

1

just put the day as first day of the month:

SELECT * FROM pagamento 
WHERE data >= 'YYYY-MM-01' 
AND data <= 'YYYY-MM-01';

1

Try something like that:

SELECT * FROM PAGAMENTO 
WHERE data >= 'YYYY-MM-DD' 
AND data <= 'YYYY-MM-DD';

Subsititui YYYY MM and DD for Year, Month and Start Day and Year Month and End Day. Usa Day 01 if you want to start the month

Solution without Informing the DIA:

    DELIMITER $

    CREATE PROCEDURE P_Busca(MESI INT, ANOI INT, MESF INT, ANOF INT)
    BEGIN
    SELECT * FROM PAGAMENTO
    WHERE data >= STR_TO_DATE(CONCAT(CAST(ANOI AS CHAR(4)),' ',CAST(MESI AS CHAR(2)),' ','1'),'%Y %c %e')
    AND data <= LAST_DAY(STR_TO_DATE(CONCAT(CAST(ANOF AS CHAR(4)),' ',CAST(MESF AS CHAR(2)),' ','05'),'%Y %c %e'));
    END $

    DELIMITER ;

To Use Procedure Call Her:

CALL P_Busca(Mes Inicial,Ano Inicial,Mes Final, Ano Final);

Example:

CALL P_BUSCA(04,2003,04,2004);

It will be from 04/2003 to 04/2004

  • The title of the question says it has to be between dates. This way it will only return the specified years and months.

  • @tvdias Please check the proposed new solution.

  • @marceloasr worked yes, only it would have to be with the month and year only?

  • You can think about using the functions: STR_TO_DATE(CONCAT('YYYY', 'MM', '01'), '%Y%m%d') ...

  • Good! That’s the basis to think about! To gain more votes and the acceptance of the AP, I suggest considering that the dates are passed by parameters and could not be manipulated as you proposed. Try creating an answer where 2 dates are passed and their days are "ignored", or receive two strings in YYYY-MM format, for example, or years and months separately.

  • Try to understand with the AP what exactly it needs.

  • Please check the new solution proposal using Procedure.

  • Take a look at https://answall.com/a/455480/172323

Show 3 more comments

1


  • 2

    You would probably have problems if the desired period was, for example, from 01/10/2019 to 29/02/2020.

  • @gojamex, worked perfectly, mto thanks to tds who helped me.

  • @Have you checked the result? Apparently this solution will only return June and July dates, and it doesn’t seem to me that’s what you need.

  • @fernandosavio was just that, because I need to choose the period of months and year I wanted, for example I can change this select to search from June until December, if it fit perfectly...mto thanks for your attention.

  • @Ragnar This answer makes no sense to search the results between dates without considering the day. This Where will return the results in the MONTHS And Selected YEARS, not the results in a date range, as asked. The only occasion that this Where can be used is when you need the results between months X and Y for the specified years (in this case also by a between).

  • Either the question is incorrect or the answer to the question is https://answall.com/a/455480/172323.

Show 1 more comment

Browser other questions tagged

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