all groups > sql server programming > november 2006 >
You're in the

sql server programming

group:

Get the hiredate which fall in the previous month


Re: Get the hiredate which fall in the previous month Arnie Rowland
11/9/2006 3:08:07 PM
sql server programming:
Something like this should work for you:


SELECT
Emp_Name =3D FirstName + ' ' + LastName,
LastName,=20
HireDate,=20
Month =3D month( dateadd( month, -1, getdate() ))
FROM Employee
WHERE ( HireDate >=3D dateadd( month, ( datediff( month, 0, getdate() =
) -1), 0 )=20
AND HireDate < dateadd( month, datediff( month, 0, getdate() ), 0 =
)=20
)

--=20
Arnie Rowland, Ph.D.
Westwood Consulting, Inc

Most good judgment comes from experience.=20
Most experience comes from bad judgment.=20
- Anonymous

You can't help someone get up a hill without getting a little closer to =
the top yourself.
- H. Norman Schwarzkopf


[quoted text, click to view]
RE: Get the hiredate which fall in the previous month Ameet Phadnis(e Tek Global Inc.)
11/9/2006 3:18:02 PM
Try this -

SELECT FirstName + ' ' + LastName AS Emp_Name, LastName, HireDate, MONTH
(GETDATE()) - 1 AS Month
FROM Employee
WHERE HireDate BETWEEN DateAdd(d, - (Day(DateAdd(m, -1, GetDate())) - 1),
DateAdd(m, -1, GetDate())) AND DateAdd(d, - (Day(GetDate())), GetDate())

--
Ameet Phadnis
Sr. Technical Consultant
e Tek Global Inc.
ASP Alliance Author Page: http://aspalliance.com/author.aspx?uId=44260


[quoted text, click to view]
Get the hiredate which fall in the previous month zwieback89 via SQLMonster.com
11/9/2006 10:52:00 PM
Hi,

How can I get all the hiredates which is falls in the previous month?

Supposed today is November 9th. So I would like to take todays' date -
GetDate()

Now I want to find the previous month's hires.

SELECT FirstName + ' ' + LastName AS Emp_Name, LastName, HireDate, MONTH
(GETDATE()) - 1 AS Month
FROM Employee

I want to add a where clause where Hire date is between the 1st and last day
of the previous month. How can I achieve this?

Thanks.

--
---------------------
zwieback89

Message posted via http://www.sqlmonster.com
Re: Get the hiredate which fall in the previous month Tracy McKibben
11/10/2006 8:44:05 AM
[quoted text, click to view]

If your employee table isn't too large, you could simply do:

SELECT
FirstName + ' ' + LastName AS Emp_Name,
LastName,
HireDate,
MONTH(HireDate) AS Month
FROM Employee
WHERE DATEDIFF(month, HireDate, GETDATE()) = -1

This will do a table or index scan, so it might not be the best approach
for a large table...


--
Tracy McKibben
MCDBA
AddThis Social Bookmark Button