Solved

SQL Query GETDATE and DATEADD

Posted on 2014-12-11
14
166 Views
Last Modified: 2014-12-11
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')
0
Comment
Question by:dmanisit
  • 6
  • 6
  • 2
14 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40494177
Isn't that what you have done with

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

Open in new window


?
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40494180
>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'.
0
 

Author Comment

by:dmanisit
ID: 40494184
I did that sir, but it is still showing me data from today forward. I only want 5 days out
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40494192
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'.
0
 

Author Comment

by:dmanisit
ID: 40494202
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.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40494209
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

0
 

Author Comment

by:dmanisit
ID: 40494217
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.
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40494228
Are you using SQL Server 2005?
0
 

Author Comment

by:dmanisit
ID: 40494233
Yes sir
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40494236
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

0
 

Author Comment

by:dmanisit
ID: 40494239
You are brilliant. Except :-) it is pulling prior dates not in advance
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 40494248
slight correction
WHERE DATEADD(dd, 0, DATEDIFF(dd, 0, APP.Appointment_DateTime)) = DATEADD(dd, 5, DATEDIFF(dd, 0, GETDATE()))

Open in new window

0
 

Author Closing Comment

by:dmanisit
ID: 40494256
Thank you so much Jim. Dates kill me in SQL
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40494266
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.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now