473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Job to be run ever first for the previous calendar month

Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.

Jan 17 '07 #1
5 2445
tolcis,

There might be a more elegant way, but this should work:

declare @Now datetime
declare @StartDate datetime
declare @EndDate datetime

set @Now = getdate()
if datepart(d, @Now) = 1 -- only execute if it is the first day of the month
begin
-- to get start date, subtract one month from the date and remove the
time from the date
set @StartDate = convert(varchar(15), dateadd (m, -1, @now), 112)
-- to get end date, remove time from date
set @EndDate = dateadd(m, 1, @StartDate)
print cast(@StartDate as char(25)) + cast(@EndDate as char(25))
-- execute your code here using >= @StartDate and < @EndDate
end

-- Bill

"tolcis" <ny********@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.

Jan 18 '07 #2
WHERE create_date < DATEADD(month,DATEDIFF(month, 0,getdate()),0)
AND create_date >= DATEADD(month,DATEDIFF(month, 0,getdate())-1,0)

Roy Harvey
Beacon Falls, CT

On 17 Jan 2007 14:30:29 -0800, "tolcis" <ny********@gmail.comwrote:
>Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.
Jan 18 '07 #3
tocis,

Ignore the first post, this is better. Schedule your job to be run on the
first of every month. This will give you the prior month date range for any
date, not just the 1st. This way if the job fails you can also run it on the
2nd, 3rd, etc.

declare @Now datetime
declare @StartDate datetime
declare @EndDate datetime

set @Now = getdate()

-- to get end date, subtract days to get to get 1st of the month for any
date
set @EndDate = dateadd(d, 1 - datepart(d,@Now), @Now)

-- remove the time portion
set @EndDate = convert(char(12), @EndDate, 112)

-- subtract a month for start date
set @StartDate = dateadd(m, -1, @EndDate)
print cast(@StartDate as char(25)) + cast(@EndDate as char(25))
-- execute your code here using >= @StartDate and < @EndDate

-- Bill

"AlterEgo" <al********@dslextreme.comwrote in message
news:12*************@corp.supernews.com...
tolcis,

There might be a more elegant way, but this should work:

declare @Now datetime
declare @StartDate datetime
declare @EndDate datetime

set @Now = getdate()
if datepart(d, @Now) = 1 -- only execute if it is the first day of the
month
begin
-- to get start date, subtract one month from the date and remove the
time from the date
set @StartDate = convert(varchar(15), dateadd (m, -1, @now), 112)
-- to get end date, remove time from date
set @EndDate = dateadd(m, 1, @StartDate)
print cast(@StartDate as char(25)) + cast(@EndDate as char(25))
-- execute your code here using >= @StartDate and < @EndDate
end

-- Bill

"tolcis" <ny********@gmail.comwrote in message
news:11**********************@11g2000cwr.googlegro ups.com...
>Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.


Jan 18 '07 #4
Roy,

Wya cool, did not know this!

-- Bill

"Roy Harvey" <ro********@snet.netwrote in message
news:vi********************************@4ax.com...
WHERE create_date < DATEADD(month,DATEDIFF(month, 0,getdate()),0)
AND create_date >= DATEADD(month,DATEDIFF(month, 0,getdate())-1,0)

Roy Harvey
Beacon Falls, CT

On 17 Jan 2007 14:30:29 -0800, "tolcis" <ny********@gmail.comwrote:
>>Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.

Jan 18 '07 #5
Thanks. Works great.
Roy Harvey wrote:
WHERE create_date < DATEADD(month,DATEDIFF(month, 0,getdate()),0)
AND create_date >= DATEADD(month,DATEDIFF(month, 0,getdate())-1,0)

Roy Harvey
Beacon Falls, CT

On 17 Jan 2007 14:30:29 -0800, "tolcis" <ny********@gmail.comwrote:
Hi!

I have a query that has to return bunch of data based on the calendar
month. I have to make sure that it will return data to me for 28 days
if it is February and for 31 if it is August(for example). I need to
be able to execute it every first of every month for the past 30, 31 or
28 days based on the calendar month. Is there a function or a stored
procedure that I can use to do that?

Thank you,
T.
Jan 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: NotGiven | last post by:
I can get the month of the date selected but not when the claendar control is first opened or when you go to the next or previous month. I'm sure it's simple, thanks for any help!
0
by: mathewgk80 | last post by:
HI all, I am having popup calendar Javascript code. But i dont know how it is connecting to asp.net code.. I am using asp.net,c#.net and also using 3tier architecture with master page.... I...
4
by: gubbachchi | last post by:
Hi all, Please anybody help me solve this problem. I am stuck up with this from past 2 weeks. I am developing an application where, when the user selects date from javascript datepicker and enters...
1
by: abhishekbrave | last post by:
The code below is opening a calendar on mouse over in the same window. I need the calendar to be opened in new window. Have to fulfill this requirement urgentely so posting the whole code here. I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.