1

I would like to get every data from a table where the year is specified. But the problem is i put my date in DATETIME type.

I'm trying something like:

SELECT
    *
FROM
    table
WHERE
    entrytime = 2017 //entrytime is datetime type.

But, it's wrong.

Is there any way to get the data or maybe a better approach ?

Thank you.

2

3 Answers 3

2

You can use year function on datetime:

SELECT *
FROM table
WHERE year(entrytime) = 2017

or better:

SELECT *
FROM table
WHERE entrytime >= '2017-01-01'
AND entrytime < '2018-01-01'

The latter can make use of index if present on the entrytime column, which can make your query run faster.

1
  • The latter is so much better than the former, I’d be tempted to forget about the former altogether
    – Strawberry
    Nov 14, 2017 at 8:08
0

You can use the year function to extract the year of a datetime:

SELECT *
FROM   mytable
WHERE  YEAR(entrytime) = 2017
0
0

You can use the YEAR function, theres a document on it here.

So...

SELECT
*
FROM Table
WHERE YEAR(DateColumn) = 2017
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.