Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Date calculations in SQL Server

Posted on 2008-06-25
1
Medium Priority
?
250 Views
Last Modified: 2010-03-19
Dear Experts,

Could anybody assist me with a good resource or some advice to calculate the following date ranges.

1) Month to date.

SELECT * FROM Xyz WHERE DateField BETWEEN '1st of the month and 'today midnight'

2) Year to date.

SELECT * FROM Xyz WHERE DateField BETWEEN '1st of Jan this year and 'today midnight'

Any help or pointers would be appreciated

Thanks

Nick
0
Comment
Question by:nkewney
[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 60

Accepted Solution

by:
chapmandew earned 2000 total points
ID: 21864295
month to date:
select * from yourtablename
where datefield >= cast(month(getdate())  as varchar(2)) + '/1/' + cast(year(getdate()) as varchar(4))
and datefield <= getdate()

year to date:

select * from yourtablename
where datefield >=  '1/1/' + cast(year(getdate()) as varchar(4))
and datefield <= getdate()
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Recently I was talking with Tim Sharp, one of my colleagues from our Technical Account Manager team about MongoDB’s scalability. While doing some quick training with some of the Percona team, Tim brought something to my attention...
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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