SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old September 11th, 2003, 11:14 AM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
convert date range to month value

I am trying to generate reports from a MS SQL 2000 database. The data is entered according to date. The date format is char and formatted like: mm/dd/yy

I need to generate monthly reports based on billing cycles that run from the 25th to the 24th of the next month. I want to create a stored procedure that runs on the 25th of each month to automatically sum the daily values into a monthly total, but I don't know how to convert the date range to a value like "September". For example:

08/25/03 - 09/24/03 = September 2003

Is there a way to do this or a better design? Any suggestions?

Eventually there will be a front end web app to this that users can go to to find outh their own totals.

Thanks.

Reply With Quote
  #2  
Old September 11th, 2003, 02:26 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
I think this will do it:

SELECT DATENAME(month, '09/24/03 ') AS 'Month Name'

SELECT DATENAME(year, '09/24/03 ') AS 'Year Name'

Vbscript also has functions that will strip out the date, like: Month(), Day(), Year().

I am not sure if I am answering your question...

Reply With Quote
  #3  
Old January 7th, 2004, 03:26 PM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Sorry I am a noob

Thanks dcarva!

But how can I mold that into a stored procedure (or vbscript) that I can schedule to run monthly and have it automatically update for the previous month?

In other words, how do I total the values for a given month each month?

I don't know if I am being clear. Sorry.

Reply With Quote
  #4  
Old January 7th, 2004, 09:46 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
You can schedule a stored procedure. Create your stored procedure. (say it's called storedProcX) Open SQL Server. Go to Management->SQL Server Agent->Jobs. Create a new job. Go to Steps tab. Click on New. Select the database you are running against. In the 'command' textbox, enter the stored proc
you want to execute...

EXEC storedProcX

Then go to the schedules tab and add a schedule.

Last edited by dcarva : January 7th, 2004 at 09:49 PM.

Reply With Quote
  #5  
Old January 12th, 2004, 02:47 PM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks again!

I think I am pretty clear on the scheduling, however, I need some help in writing the proc, or scrapping that and writing the vbscript. I have a table with the following columns:
Date(char)
Cust(char)
Size(numeric)

This table is updated on a daily basis with the day's "size" total. What I want to do is run a proc each month that would total the daily "size" into a monthly total(sum) for each Customer. The only caveat is that a "monthly" total means from the 25th of the previous month to the 24th of the current month. For example, January 2004 = '12/25/03' - 01/24/04.

If possible, Iwould like to take the totals each month and write them to a different table.

Thanks again for your help!

Last edited by craggar : January 12th, 2004 at 03:35 PM.

Reply With Quote
  #6  
Old January 12th, 2004, 10:16 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
Here is something I threw together. I would run this query in SQL analyzer to test it. Here is the code I think might work for you. This would have been much easier to write if you had stored your date fields as datetime. After you test it, throw it in a stored proc.

DECLARE @CurDate datetime
DECLARE @StartMonth int
DECLARE @StartYear int
DECLARE @StartDate varchar(10)
DECLARE @EndDate varchar(10)
SET @CurDate = GetDate()

-- Get last month and the year that last month was in
SET @StartMonth = DATEPART( month, GetDate() )
SET @StartYear = DATEPART( year, GetDate() )

-- If we are currently in January, then last month was in last year
IF ( @StartMonth = 1 )
BEGIN
SET @StartMonth = 12
SET @StartYear = ( @StartYear - 1 )
END
ELSE
BEGIN
SET @StartMonth = @StartMonth - 1
END

-- Set start and end dates
SET @StartDate = CONVERT( varchar(2), @StartMonth ) + '/25/' + CONVERT( varchar(4), @StartYear )
Print @StartDate
SET @EndDate = CONVERT( varchar(2), DATEPART( month, @CurDate ) ) + '/24/' + CONVERT( varchar(4), DATEPART( year, @CurDate ) )
Print @EndDate

-- Display customer and
SELECT Cust, SUM([size])
FROM Table1
WHERE CONVERT( DateTime, [Date]) >= CONVERT( DateTime, @StartDate )
AND CONVERT( DateTime, [Date]) <= CONVERT( DateTime, @EndDate )
GROUP BY Cust

Last edited by dcarva : January 13th, 2004 at 10:21 AM.

Reply With Quote
  #7  
Old January 13th, 2004, 11:35 AM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for the quick (and working) response. I hope you are not getting sick of me yet, but when I ran the script it gave me 2 columns, client and size. Now, when I run it today, 1/13/2004, what dates is the size value good for? Is it last month, December '03? Just curious.

Also, can I just add another few lines to update another table with this output? Update existing table with new values.

Thanks, again!

Reply With Quote
  #8  
Old January 13th, 2004, 11:51 AM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
This should display the date for you:

-- Display customer and
SELECT Cust, [Date], SUM([size])
FROM Table1
WHERE CONVERT( DateTime, [Date]) >= CONVERT( DateTime, @StartDate )
AND CONVERT( DateTime, [Date]) <= CONVERT( DateTime, @EndDate )
GROUP BY Cust, [Date]

As far as updating the other table, you can probably do an INSERT INTO...

INSERT INTO MyTable (Cust, Size)
SELECT FROM ....

(look in SQL Books ONLINE help that comes with SQL Server)

Reply With Quote
  #9  
Old January 14th, 2004, 09:50 AM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks for all your help!

Reply With Quote
  #10  
Old January 14th, 2004, 10:47 AM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
No problem. Did it work for you or are you stuck?

Reply With Quote
  #11  
Old January 14th, 2004, 11:24 AM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Oh it is working for me - I just need to work on the insert and then the interface.

One quick question on the revised version you posted. When I run that one it now gives me a date column. That is good but all the dates are different. I ran the script on the 13th, and it outputs many different dates. Is it just puuling dates from my table's date column, or maybe the last date anything was updated? It was just kind of confusing. For example:

blah 12/27/03 15236
blah 12/30/03 166
blah 12/25/03 1515
blah 01/11/04 325
blah 01/01/04 6971392
blah 01/10/04 3635201
blah 01/11/04 3627402
blah 01/09/04 625663
blah 01/09/04 660205
blah 12/25/03 78
blah 12/31/03 629801
blah 12/28/03 56379
blah 12/27/03 2052310

Any thoughts on why the different dates?

Reply With Quote
  #12  
Old January 14th, 2004, 04:19 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
Actually, adding the date messes up the query. Let me look at it further. Try adding this to your select statement:

SELECT Cust, SUM([size]), MIN([Date]) StartDate, MAX([Date]) EndDate

Reply With Quote
  #13  
Old January 14th, 2004, 09:38 PM
craggar craggar is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 7 craggar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I added what you posted to the select statement and here is the output...

blah 46218 12/27/03 12/27/03
blah 356353 01/07/04 01/07/04
blah 286225 01/11/04 01/11/04
blah 326764 01/05/04 01/05/04
blah 213198 01/06/04 01/06/04
blah 231402 12/31/03 12/31/03
blah 696 01/08/04 01/08/04
blah 2374 01/04/04 01/04/04
blah 55297 01/08/04 01/08/04
blah 595 12/28/03 12/28/03
blah 220064 01/12/04 01/12/04
blah 32646 01/05/04 01/05/04

The start date and end dates seem to match. Is that what you intended?

Reply With Quote
  #14  
Old January 14th, 2004, 09:42 PM
dcarva's Avatar
dcarva dcarva is offline
Contributing User
ASP Free Novice (500 - 999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 633 dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level)dcarva User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 9 h 18 m 20 sec
Reputation Power: 6
Yes

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > convert date range to month value


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |