I have a table(e.g. tableA) like this:
| Name | Startdate | Enddate | |---------------------------------| | a | 2012-07-01 | 2013-06-30 | | b | 2011-05-01 | 2012-04-30 | | c | 2010-01-01 | 2013-12-31 | | d | 2013-01-01 | 2014-12-31 | | e | 2011-07-01 | 2012-06-30 |
I want to get a name from the table who is active between 2012-05-01
and 2012-10-31
. From above table the result should be a,c and e.
What I have done is as following:
SELECT Name FROM tableA WHERE startdate<='2012-05-01' AND enddate>='2012-10-31'
But I am not getting the correct result.
DATETIME
- and that format isyyyyMMdd
- so try:...WHERE startdate <= '20120501' AND enddate >= '20121031'
. The format with the dashes is not safe and can be misinterpreted depending on the language setting of your SQL Server. Plus your comparison operators should be reversed, too - it should be greater or equal to20120501
and smaller or equal to20121031
- right? – marc_sYYYY-MM-DD
- they can be ambiguous to SQL Server. Better to just useYYYYMMDD
with no separator characters. – Damien_The_UnbelieveryyyyMMDD
– Himanshu Jansari