Solved

Date calculations in SQL Server

Posted on 2008-06-25
1
209 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
1 Comment
 
LVL 60

Accepted Solution

by:
chapmandew earned 500 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Read about achieving the basic levels of HRIS security in the workplace.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

948 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

18 Experts available now in Live!

Get 1:1 Help Now