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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select Statement

Status
Not open for further replies.

IAMINFO

MIS
Feb 21, 2002
62
US
Hello everyone I am trying to write this select statement in sql
The report should be for those patients admitted more than 2 days prior but less than 7 days prior to the date of the report.

I have just started learning sql, please help if you can.
I am not sure if I am writing this correctly.

Thank you for reading my post.

Select * from admissions
Where admission_date >= getdate() -2 and <= getdate()-7
 
Code:
declare @Today datetime()

set @Today = getdate()

Select * from  admissions
Where admission_date < DATEAdd(dd,-2,@Today) and Addmission_Date >= dateadd(dd,-7,@Today)
 
No, WHERE clause is NOT right.
There NO such date that could be BIGGER than 2 days ago but SMALLER than 7 days ago :).
Also you may take attention to time portion of GETDATE(). What if you run query today at 5 PM? Did you want all the patients admitted on 17 Sept. 2009 4PM to be ignored?
So:
Code:
DECLARE @ToDay datetime
SET @ToDay = DATEADD(dd, DATEDIFF(dd, 0, GETDATE()), 0)
Select *
       from  admissions
Where admission_date >= @ToDay - 7 AND
      admission_date <  @ToDay - 1 -- NO EQUAL sign here

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Yes, I wanted to pointed this out - we need to make @Today time independent, but somehow I could not 100% remember this and have to check Tibor's blog everytime.

Just too busy at the moment to write long explanations.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top