Solved

SQL Query GETDATE and DATEADD

Posted on 2014-12-11
14
180 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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
 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

778 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