I have a table defined like this
empid fromdate todate office_ID
1 2012-03-01 2014-04-2 1
1 2014-04-03 ---- 2
2 2012-03-01 2014-04-2 2
3 2012-03-01 2014-04-2 1
If the employee is currently working at some office then todate
will be not known hence it is null. This logic can be changed if you suggest something better.
From this table I want to fetch an employee's location(s) for a given month. For example, for May 2005 where was employee 1? For that I have designed this query
select *
from tbl_emp_office
where (year(From_date)*100 + month(From_date)) < '201505'
This structure is not giving the exact solution to my problem. Can you suggest more suitable structure for this?
EDIT
fromdate
has data type Date.
Best Answer
What "datatype" is
From_date
? Assuming it is DATE or DATETIME or TIMESTAMP, then this will find records before May, 2015:This will find records for only May, 2015:
If you are using VARCHAR, well don't.
Edit to answer "Where was emp#1 working during May, 2015?"
Note: If he worked in multiple offices during May, this will produce multiple rows.