Only catch the year of a column in Mysql

Asked

Viewed 1,212 times

1

I have a query that has a condition, which checks whether the record is greater than or equal to today’s date.

He is like that:

WHERE t1.bidpack_buy_date => NOW()

Well, then he’ll check if the column value bidpack_buy_date is greater than or equal to the current date and time.

In the column bidpack_buy_date it gets recorded that way:

2018-01-10 09:00:00

I need that in this condition, it checks only the day, month and year. That is, it will be valid only if it is equal to today’s date or later, the time should be considered.

How can I do that?

2 answers

2


If you want to check individual, you can use

WHERE YEAR(bidpack_buy_date) => YEAR(NOW())

or

WHERE MONTH(bidpack_buy_date) => MONTH(NOW())

To check the date without the time, then:

WHERE DATE(bidpack_buy_date) => DATE(NOW())

Similarly, to check only the time:

WHERE TIME(bidpack_buy_date) => TIME(NOW())

  • 1

    But aia it will be separated, have to check the year, month and day in the same condition?

  • I edited the answer with that information too, Wendler! :)

  • Can you tell me if I did it right, and if the query is correct?

  • 1

    DELETE t1 FROM bid_account t1 INNER JOIN bid_account t2 WHERE t1.id > t2.id AND t1.bidding_price = t2.bidding_price AND t1.auction_id = '" . $obj->auction_id . "' AND t1.bidding_type = 's' AND t1.bid_flag = 'd' AND DATE(t1.bidpack_buy_date) => DATE(NOW())

  • I think that’s how it is, Wendler! Make a copy of this table and make a test not to affect if you are in production rs

  • It worked, Wendler?

  • 1

    You did @Cleber Griff, thank you very much! =)

Show 2 more comments

1

I believe the question has already been answered but another way to do it is by using the function DATE_FORMAT():

SELECT * FROM tabela WHERE DATE_FORMAT($data, "%Y%m%d") > DATE_FORMAT(NOW(), "%Y%m%d");

Note: The link has the options for formatting the string

If it has a difference of performance I can not say, but it should not be exorbitant

Browser other questions tagged

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