Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date calculation in SQL

Status
Not open for further replies.

Deam

Programmer
Oct 10, 2000
68
0
0
US
In my query I need to include all the records for a Person that fall between today and two weeks back. The table I am selecting records from has just one date field - PeriodStartDate

Appreciate any help
 
What datatype is PeriodStartDate? Is it VARCHAR or DATETIME?

-SQLBill

Posting advice: FAQ481-4875
 
Yup, that query works OK on Mondays :)

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Wy do I keep on getting
Line 3: Incorrect syntax near '<'.

When I run SQL Dennis's query ??
 
Depends on definition of "two weeks back". Does it mean:

a) 14 days back, time value included
b) 14 days back, time value excluded (from 00:00AM)
c) during last two days (starting with Sunday or Monday, depending on language settings)

?

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
It is SQL server. Two weeks would be 14 days back from the time the query is run.

Basically this is a sproc that is used to create a Crystal report. The Sproc needs to pull records of employees whose PeriodStartdate falls with the two weeks of the date the report is run

Thanks !
 
Then try:

Code:
...
where PeriodStartDate >= dateadd(wk, -2, getdate()) and PeriodStartDate <= getDate()

------
"There's a man... He's bald and wears a short-sleeved shirt, and somehow he's very important to me. I think his name is Homer."
(Jack O'Neill, Stargate)
[banghead]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top