aabujamra
aabujamra

Reputation: 4636

Iterating in a date column in SQL

MySQL rookie here.

I have this table:

+-------------+-----------------------+
| business_id |     date_creation     |
+-------------+-----------------------+
| '3610'      | '2012-08-19 07:43:05' |
| '5020'      | '2012-08-18 03:38:51' |
| '5020'      | '2012-08-18 03:38:51' |
| '9491'      | '2017-08-18 06:52:52' |
| '10389'     | '2012-08-18 07:08:50' |
| ...         |                       |
+-------------+-----------------------+

I'm getting that through this query:

SELECT
  citation.business_id,
  citation.date_creation
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id;

However, I want to filter only the dates from the year 2017.

Upvotes: 0

Views: 41

Answers (2)

tarking
tarking

Reputation: 11

You can add an AND clause to compare the date column value to a date literal.

SELECT
  citation.business_id,
  citation.date_created
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id
AND citation.date_created >= '2017-01-01 00:00:00';

Upvotes: 1

Taher A. Ghaleb
Taher A. Ghaleb

Reputation: 5240

You can update your query like this:

SELECT
  citation.business_id,
  citation.date_created
FROM citation,
     business
WHERE primary_activity_id = 850
AND business.id = citation.business_id
AND YEAR(citation.date_created) >= 2017;

Upvotes: 1

Related Questions