Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
Search :       Advanced Search »
Home » Blogs Home » Blog Detail
How to get all dates between date range in sql query
 by Nipun Tomar on Jun 05, 2008

This sql query will get all the dates beween given date range.
Comments: 1 Views: 20394     Printable Version

DECLARE @StartDate DATETIME,@EndDate DATETIME

SELECT @StartDate = '20080101',@EndDate = '20080930'

SELECT DATEADD(day, z.num, @StartDate)

FROM (

SELECT b10.i + b9.i + b8.i + b7.i + b6.i + b5.i + b4.i + b3.i + b2.i + b1.i + b0.i num

FROM (SELECT 0 i UNION ALL SELECT 1) b0

CROSS JOIN (SELECT 0 i UNION ALL SELECT 2) b1

CROSS JOIN (SELECT 0 i UNION ALL SELECT 4) b2

CROSS JOIN (SELECT 0 i UNION ALL SELECT 8) b3

CROSS JOIN (SELECT 0 i UNION ALL SELECT 16) b4

CROSS JOIN (SELECT 0 i UNION ALL SELECT 32) b5

CROSS JOIN (SELECT 0 i UNION ALL SELECT 64) b6

CROSS JOIN (SELECT 0 i UNION ALL SELECT 128) b7

CROSS JOIN (SELECT 0 i UNION ALL SELECT 256) b8

CROSS JOIN (SELECT 0 i UNION ALL SELECT 512) b9

CROSS JOIN (SELECT 0 i UNION ALL SELECT 1024) b10

) z

WHERE z.num <= DATEDIFF(day, @StartDate, @EndDate)

ORDER BY z.num

*Title:  
*Comment:
 
 

get all dates between date range using CTE by Nipun Tomar On Jun 05, 2008

This can also be achieved using CTE. A Common Table Expression is a temporary result set that exists only within the scope of a single SQL statement.

with CTE(d)as(select d = convert(datetime,'20080101')

union all select d = d + 1 from CTE where d < '20081231')

SELECT d FROM CTE

option (maxrecursion 370)


 Blogger's Profile
Age: Not Available
Location:
Title: Project Lead
Joined: Feb 18, 2005
Education: Master's Degree
 More Blogs from this Blogger
Development Fact
RPC server unavailable error - Microsoft Internet Connection Sharing (ICS)
Can not create open silverlight 2 projects "Object reference not set to a instance of an object"
Stop ASP.NET web.config inheritance
Recycle IIS Application Pool
Windows Installer CleanUp Utility
Typical Definitions
Enable Windows Search Service in Windows Server 2008
Uninstalling Windows Internal Database
How to get all dates between date range in sql query
View all »
 Latest Blogs
creater
asp.net,C#.net,sqlserver,WCF,VS2008
How to change the color of a caret in a textbox in silverlight
AsEclipse--VS 05/08 add-in
GetNames and GetValues for Enums in Silverlight
Advanced Persistent Threats(APT)
Country list
Insert Only Digits In TextBox
Binding your MVVM model to an AttachedProperty
MVP Summit 2010 - Who is going?
View all »
 Latest Articles
Image Transformation in WPF
Convert Byte Array to Double in C#
GrayScale Image in WPF
Cropping or Clipping in WPF
Clipping or Cropping Images in WPF
DataGrid and Datalist Controls in ASP.NET
Drawing Other Graphics Shapes by Applying Cap and Dashed Line Styles in GDI+
Creating message body by parsing document from Document Library and sending mail in SharePoint 2007
TraceListener Classes in C#
Convert Double to Byte Array in C#
View all »

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2010  Mindcracker LLC. All Rights Reserved