Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Declare Date range for multiple SQL queries in series

Posted on 2013-12-12
1
Medium Priority
?
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
chaau earned 1500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

670 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