Solved

Date calculations in SQL Server

Posted on 2008-06-25
1
198 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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…

772 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

14 Experts available now in Live!

Get 1:1 Help Now