Solved

Declare Date range for multiple SQL queries in series

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

12 Experts available now in Live!

Get 1:1 Help Now