Solved

Date calculations in SQL Server

Posted on 2008-06-25
1
242 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 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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

719 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