Solved

Declare Date range for multiple SQL queries in series

Posted on 2013-12-12
1
312 Views
Last Modified: 2013-12-23
I have a series of queries where I need to declare a date range, across the multiple queries there are two different fields that will need to use the date range (if that makes any difference).

WHERE CONVERT(DATETIME, DateOfService, 101) BETWEEN '2013-06-01' AND '2013-12-31'

WHERE CONVERT(DATETIME, CHARTED_DATE, 101) BETWEEN '2013-06-01' AND '2013-12-31'

How do I use the declare syntax to describe the between XYZ and ABC range?
0
Comment
Question by:ghettocounselor
1 Comment
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39715722
Use this syntax:
DECLARE @XYZ DATETIME
DECLARE @ABC DATETIME
SET @XYZ =  '20130601' 
SET @ABC =  '20131231'

' OR use one line assignment like this:
' SELECT @XYZ =  '20130601', @ABC =  '20131231'

WHERE CONVERT(DATETIME, DateOfService, 101) BETWEEN @XYZ AND @ABC

WHERE CONVERT(DATETIME, CHARTED_DATE, 101) BETWEEN @XYZ AND @ABC

Open in new window

BTW, if the DateOfService and CHARTED_DATE are already datetime columns in your database, you do not need CONVERT statement, you can use them directly, like this:
WHERE DateOfService BETWEEN @XYZ AND @ABC

WHERE CHARTED_DATE BETWEEN @XYZ AND @ABC

Open in new window

This way it even be faster, as SQL Server will be able to use indexes
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

914 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

13 Experts available now in Live!

Get 1:1 Help Now