Solved

Date calculations in SQL Server

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VMWare environment audit 8 110
Are triggers slow? 7 25
SQL Get Store Procedure Column Name As Row 5 41
SSIS Loading Excel file to SQL database 10 22
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.
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.
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…

726 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