user3064132 user3064132 - 3 months ago 7
SQL Question

get all date from selected year

I have some value like this :

from to
2012-04-01 2013-12-31
2014-01-01 2016-08-10
2016-08-11 2016-12-28
2016-12-30 2017-12-20
2017-12-21 2018-12-11


How can I get all value when i select 2016 year . I want to get this :

from to
2014-01-01 2016-08-10
2016-08-11 2016-12-28
2016-12-30 2017-12-20


Currently my sql just get the value if both from and to in 2016

.... WHERE From>='2016-01-01' AND To<='2016-12-31'


Note: it's an example date , so please don't post answer like this :

...WHERE From>='2014-01-01' AND To<='2017-12-20'

Answer

You can use the YEAR function:

SELECT from,
       to
FROM yourTable
WHERE YEAR(from) = 2016 OR
      YEAR(to)   = 2016

This will retain any record which has either a from date or a to date which lies in the 2016 calendar year.

Comments