I want a dynamic query which will fetch the employee name who has completed successfully 6 months and are ready for confirmation.
I have written a hardcode query does that. Here is the query
SELECT [emp_name] FROM [emp_mst]
WHERE Dt_Of_Join >= dateadd(m, -7, datediff(d, 8, getdate()))
and Dt_Of_Join < dateadd(m, -6, datediff(d, 9, getdate()))
For example: if employee has Joined on 1st dec,2014
, he will complete 6 months on 31st May 2015
.
So in june he can be intimated on any date. The above query gives me the result for Nov16 2014
to Dec 15 2014
, based on today's date. I want it to be handled dynamically
Please suggest.
I am using sql server 2005.
Best Answer
Today, without time, is:
Then to move to the first day of this month:
Now to get all employees who started within the month 6 months earlier, you can say:
The smart way to do this would be through a parameter, which falls back to today if no date is supplied.
(Now it's also very easy to test that this works for any date, including February 29th in leap years...)
Here is the illogical way to do it in one line (I have absolutely no clue why you think this is a valid or important requirement):
Oh yeah, that's much easier to read and troubleshoot. Also, it didn't save you any characters, it's actually more...
</facepalm>