Solved

Someone Help me Writing Date Script

Posted on 2009-04-09
5
180 Views
Last Modified: 2012-05-06
I want to have startdate & enddate for 2009

Startdate should be Jan 31st 2009
EndDate should be March 31st 2009(Since Last Accounting Month is March)

Since, i need to use these dates in While Loop to run Temp Table.
Since for Every month end I need to run Procedure. So, i need to insert the code for StartDate & EndDate

I already wrote Below Code, but it doesnt work for March...The code is taking March 28th instead of March 31st, Since, Feb Month runs for 28th.

Declare @StartDate datetime
Declare @EndDate datetime

Set @EndDate = CONVERT(varchar,DATEADD(d,-DAY(GetDate()),GetDate()),101)
Set @StartDate = '1/31/'+ CAST(YEAR(@EndDate)as Char(4))

--Print @StartDate
--Print @EndDate

--Create Table

While @StartDate <= @EndDate
Begin

--Inserting Data into table

set @StartDate = dateadd(m, 1, @StartDate)
End
0
Comment
Question by:bsumanrao
[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
5 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24110481
Try like this:



Set @StartDate = DATEADD(month, 0, DATEDIFF(day, 0, GETDATE()) - DATEPART(dy, GETDATE()) + 1)Set @EndDate = DATEADD(month, 0, DATEDIFF(day, 0, GETDATE()) - DAY(GETDATE()) + 1)
 
WHILE @StartDate < @EndDate
...

Open in new window

0
 
LVL 25

Accepted Solution

by:
reb73 earned 250 total points
ID: 24110483
Try this -
Declare @StartDate datetime
Declare @EndDate datetime
Declare	@WorkDate datetime
 
Set @EndDate = CONVERT(varchar,DATEADD(d,-DAY(GetDate()),GetDate()),101)
Set @WorkDate =  '01/01/'+ CAST(YEAR(@EndDate)as Char(4))
--Print @StartDate
--Print @EndDate
 
--Create Table
 
While @WorkDate < @EndDate
Begin
	Set @StartDate = DATEADD(d, -1, DATEADD(m, 1, @WorkDate))
 
--Inserting Data into table
 
	set @WorkDate = DATEADD(m, 1, @WorkDate)
End

Open in new window

0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 24110523
Sorry just caught the January 31st start.  PRevious code gets January 1st.  It would help to know how you were using the date later to make sure my suggestion will work correctly.  I do this sort of analysis with dynamic dates where I am doing _date >= @start_date and _date < @end_date so it works using dates at midnight.
0
 

Author Closing Comment

by:bsumanrao
ID: 31568683
Thanks  Reb........
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24110579
I don't think that the dateadd(m...) will work because adding a month to January 31 gives you Feb-28, but 1 month later is March 28.

Try this, which returns the start as day 1, and end as day 1 of next month.  If you depend upon enddate being the last day of the month, instead of the first day of next month then use the second script.

The difference would be:

SomeField between @Start and @End

vs

SomeField >= @start
and SomeField < @End
Declare 
      @StartDate    datetime
     ,@EndDate      datetime
 
set @startdate = ltrim(str((datepart(yy,getdate())*10000)+101))
set @enddate = dateadd(m,1,@startdate)
while @enddate < getdate()
begin
     select @startdate,@enddate
 
set @startdate = dateadd(m, 1,@startdate)
set @enddate = dateadd(m,1,@startdate)
end
 
 
go
 
Declare 
      @StartDate    datetime
     ,@EndDate      datetime
 
set @startdate = ltrim(str((datepart(yy,getdate())*10000)+101))
set @enddate = dateadd(m,1,@startdate)-1
while @enddate < getdate()
begin
     select @startdate,@enddate
 
set @startdate = dateadd(m, 1,@startdate)
set @enddate = dateadd(m,1,@startdate)-1
end

Open in new window

0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will use…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 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