SQL rule to filter only one month in TIMESTAMP field

0

Hello. I have a stock drive in month 1, month 2, month 3, month 4, month 5 ... how do I create the rule for only the stock movements to appear in month 3? Remember that the field is TIMESTAMP.

    
asked by anonymous 05.10.2018 / 12:40

1 answer

1

There are several ways to do it. Some of them:

Using BETWEEN to pull a range:

SELECT *
FROM tabela
WHERE timestamp BETWEEN '01/03/2018 00:00:00' AND '31/03/2018 23:59:59'

Using MONTH to filter only the month:

SELECT *
FROM tabela
WHERE MONTH(timestamp) = 3

Using MONTH and YEAR to filter month and year:

SELECT *
FROM tabela
WHERE MONTH(timestamp) = 3
AND YEAR(timestamp) IN (2017,2018)

Very good and complete documentation: Date and Time Functions - MySQL

    
05.10.2018 / 12:42