Link to home
Start Free TrialLog in
Avatar of dmanisit
dmanisit

asked on

SQL Query GETDATE and DATEADD

I have a query and instead of pulling the between date I want to pull 5 days in advance only

USE Ntier_JSA
Select C.First_Name, C.Last_Name, A.Phone, cast(datepart(mm,APP.Appointment_DateTime) as varchar(2))+'/'+cast(datepart(dd,APP.Appointment_DateTime) as varchar(2))+'/'+ cast(datepart(yyyy,APP.Appointment_DateTime) as varchar(4)) as Appointment_Date,
case when len(cast(datepart(hour, APP.Appointment_DateTime) as varchar(2)))=1 then '0' + cast(datepart(hour, APP.Appointment_DateTime) as varchar(2)) else cast(datepart(hour, APP.Appointment_DateTime) as varchar(2)) end + ':'+
case when len(cast(datepart(minute, APP.Appointment_DateTime) as varchar(2)))=1 then '0' + cast(datepart(minute, APP.Appointment_DateTime) as varchar(2)) else cast(datepart(minute, APP.Appointment_DateTime) as varchar(2)) end + ':'+
case when len(cast(datepart(second, APP.Appointment_DateTime) as varchar(2)))=1 then '0' + cast(datepart(second, APP.Appointment_DateTime) as varchar(2)) else cast(datepart(second, APP.Appointment_DateTime) as varchar(2)) end as Appointment_Time,
 P.Patient_Number AS Client_Account_Number,
 APP.Resource_ID AS Provider_Number, APP.Appointment_Type_ID AS Procedure_Number, SL.Scheduling_Location_ID AS Location_Number, APP.Comments AS Appointment_Notes,
R.Description AS Provider_Name, '' AS Procedure_Name, SL.Description AS Location_Name, '' AS Message_Name, C.Cell_Phone, '' AS Send_SMS, LOC.Phone AS Caller_ID_Number,
C.Email_Address, '' AS SEND_EMAIL, A.Street1 AS Client_Address, A.City AS Client_City, A.State AS Client_State, A.Zip_Code AS Client_ZipCode, C.Work_Phone AS Alternate_Client_Phone,
'' AS Client_Info_Notes, '' AS Never_Call, PI.Field_Value AS Language, '' AS Send_Fax, '' AS Volume_Level
FROM PM.Contacts C
INNER JOIN PM.Patients P WITH (NOLOCK) ON P.Contact_ID = C.Contact_ID
 INNER JOIN PM.Addresses A WITH (NOLOCK) ON A.Address_ID = C.Address_ID
INNER JOIN PM.Appointments APP WITH (NOLOCK) ON APP.Patient_ID = P.Patient_ID
INNER JOIN PM.Patient_Info PI WITH (NOLOCK) ON PI.Patient_ID = P.Patient_ID
INNER JOIN PM.Resources R WITH (NOLOCK) ON R.Practitioner_ID = P.PCP_Practitioner_ID
INNER JOIN PM.Scheduling_Locations SL WITH (NOLOCK) ON SL.Scheduling_Location_ID = APP.Scheduling_Location_ID
INNER JOIN PM.Locations LOC WITH (NOLOCK) ON LOC.Location_ID = SL.Location_ID
WHERE APP.Appointment_DateTime between GETDATE() and DateAdd(d,5,GetDate()) AND APP.Status = 'S'
AND PI.Field_Value IN ('English', 'Spanish', 'French', 'Portuguese', 'Chinese', 'Vietnamese', 'Greek', 'Russian', 'Other')
Avatar of Phillip Burton
Phillip Burton

Isn't that what you have done with

 between GETDATE() and DateAdd(d,5,GetDate())

Open in new window


?
Avatar of Jim Horn
>USE Ntier_JSA
Add a 'GO' to the line below any USE statement.

>I have a query and instead of pulling the between date I want to pull 5 days in advance only
>WHERE APP.Appointment_DateTime between GETDATE() and DateAdd(d,5,GetDate())

The above works if you want to include the time component of GETDATE().
If this doesn't work for you, then it's not clear what you mean by '5 days in advance only'.
Avatar of dmanisit

ASKER

I did that sir, but it is still showing me data from today forward. I only want 5 days out
Today is 2014-12-11.   Please verify that your question is 'I have a query that returns data from 2014-12-11 10:30:00' to '2014-12-16 10:30:00'.   I want it to return data only from 2014-12-16'.
I am sorry for being unclear. I want the Query to ONLY return the Appointments for 5 days from now. So next Tuesday only not anything between it.
Ok.  Change this part of your SQL statement
WHERE APP.Appointment_DateTime between GETDATE() and DateAdd(d,5,GetDate())

Open in new window

... to this...
WHERE CAST(APP.Appointment_DateTime as date) = DATEADD(d, 5, CAST(GETDATE() as date)) 

Open in new window

Msg 243, Level 16, State 1, Line 2
Type date is not a defined system type.
Msg 243, Level 16, State 1, Line 2
Type date is not a defined system type.
Are you using SQL Server 2005?
Yes sir
The date data type was introduced in SQL 2008, so if you're running an earlier version we'll need another way.

Try this..
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, APP.Appointment_DateTime)) = DATEADD(dd, 0, DATEDIFF(dd, 5, GETDATE()))

Open in new window

You are brilliant. Except :-) it is pulling prior dates not in advance
ASKER CERTIFIED SOLUTION
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you so much Jim. Dates kill me in SQL
Thanks for the grade.

Might not be a bad idea to inform your client at an opportune time that they're running SQL Server four versions old (2014 > 2012 > R2 > 2008 > 2005), and that an added cost in doing that is that most experts are not current in SQL 2005-specific issues, which means they're either going to have to pay top dollar to get a veteran rock star (who won't want to work on outdated versions of SQL if they had a choice), or settle for not very good developers to maintain it.

Not to mention no idea if Microsoft supports SQL 2005 anymore.