Solved

Declare Date range for multiple SQL queries in series

Posted on 2013-12-12
1
321 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

759 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