PC Review


Reply
Thread Tools Rate Thread

Assemble monthly periods between dates into a string

 
 
XP
Guest
Posts: n/a
 
      30th Jul 2008
The program collects a BEGIN date and an END date from the user;
these are stored in variables as dates: mdPeriodBEG and mdPeriodEND;
no problem.

I now need to construct a string variable that incorporates all the
monthly periods in between, regardless of start and end dates given,
in a certain format. Examples:

1. Example: Given BEG: 10-2007; END: 02-2008; *

I need a string variable loaded with:
'Oct-07', 'Nov-07', 'Dec-07', 'Jan-08', 'Feb-08'

*Please note: crosses a calendar year

2. Example: Given BEG: 02-2008; END: 05-2008;

I need a string variable loaded with:
'Feb-08', 'Mar-08', 'Apr-08', 'May-08'

This format is required so I can ultimately feed this string variable of
dates into a SQL string. I know how to concatenate a string variable,
but I need an efficient method of doing so using variable start and end
periods.

Please note that due to the datatype being used in Oracle, I cannot
simply use BETWEEN in my SQL string, rather I must feed it each
individual period as shown in my example using the IN keyword.

I am at a loss on how to do this efficiently, but perhaps a select
case or two?

Thanks much in advance for your kind assistance.

 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      30th Jul 2008
Of course, 10-2007 and 02-2008 are not **real** VB dates; however, this
function should return the SQL string you are looking for whether you input
your dates like you showed in your message or as real VB dates...

Function GetSQLDates(mdPeriodBEG As Date, mdPeriodEND As Date) As String
Dim X As Long
GetSQLDates = "'" & Format(mdPeriodBEG, "mmm-yy") & "'"
For X = 1 To DateDiff("m", mdPeriodBEG, mdPeriodEND)
GetSQLDates = GetSQLDates & ", '" & _
Format(DateAdd("m", X, mdPeriodBEG), "mmm-yy") & "'"
Next
End Function

Rick


"XP" <(E-Mail Removed)> wrote in message
news:3A369ADA-537D-47A8-B0D8-(E-Mail Removed)...
> The program collects a BEGIN date and an END date from the user;
> these are stored in variables as dates: mdPeriodBEG and mdPeriodEND;
> no problem.
>
> I now need to construct a string variable that incorporates all the
> monthly periods in between, regardless of start and end dates given,
> in a certain format. Examples:
>
> 1. Example: Given BEG: 10-2007; END: 02-2008; *
>
> I need a string variable loaded with:
> 'Oct-07', 'Nov-07', 'Dec-07', 'Jan-08', 'Feb-08'
>
> *Please note: crosses a calendar year
>
> 2. Example: Given BEG: 02-2008; END: 05-2008;
>
> I need a string variable loaded with:
> 'Feb-08', 'Mar-08', 'Apr-08', 'May-08'
>
> This format is required so I can ultimately feed this string variable of
> dates into a SQL string. I know how to concatenate a string variable,
> but I need an efficient method of doing so using variable start and end
> periods.
>
> Please note that due to the datatype being used in Oracle, I cannot
> simply use BETWEEN in my SQL string, rather I must feed it each
> individual period as shown in my example using the IN keyword.
>
> I am at a loss on how to do this efficiently, but perhaps a select
> case or two?
>
> Thanks much in advance for your kind assistance.
>


 
Reply With Quote
 
XP
Guest
Posts: n/a
 
      30th Jul 2008
Hi Rick,

I ultimately came up with a solution, but it's not nearly as cohesive and
efficient as yours; I am adding your solution to my code library, thanks much
for the help!



"Rick Rothstein (MVP - VB)" wrote:

> Of course, 10-2007 and 02-2008 are not **real** VB dates; however, this
> function should return the SQL string you are looking for whether you input
> your dates like you showed in your message or as real VB dates...
>
> Function GetSQLDates(mdPeriodBEG As Date, mdPeriodEND As Date) As String
> Dim X As Long
> GetSQLDates = "'" & Format(mdPeriodBEG, "mmm-yy") & "'"
> For X = 1 To DateDiff("m", mdPeriodBEG, mdPeriodEND)
> GetSQLDates = GetSQLDates & ", '" & _
> Format(DateAdd("m", X, mdPeriodBEG), "mmm-yy") & "'"
> Next
> End Function
>
> Rick
>
>
> "XP" <(E-Mail Removed)> wrote in message
> news:3A369ADA-537D-47A8-B0D8-(E-Mail Removed)...
> > The program collects a BEGIN date and an END date from the user;
> > these are stored in variables as dates: mdPeriodBEG and mdPeriodEND;
> > no problem.
> >
> > I now need to construct a string variable that incorporates all the
> > monthly periods in between, regardless of start and end dates given,
> > in a certain format. Examples:
> >
> > 1. Example: Given BEG: 10-2007; END: 02-2008; *
> >
> > I need a string variable loaded with:
> > 'Oct-07', 'Nov-07', 'Dec-07', 'Jan-08', 'Feb-08'
> >
> > *Please note: crosses a calendar year
> >
> > 2. Example: Given BEG: 02-2008; END: 05-2008;
> >
> > I need a string variable loaded with:
> > 'Feb-08', 'Mar-08', 'Apr-08', 'May-08'
> >
> > This format is required so I can ultimately feed this string variable of
> > dates into a SQL string. I know how to concatenate a string variable,
> > but I need an efficient method of doing so using variable start and end
> > periods.
> >
> > Please note that due to the datatype being used in Oracle, I cannot
> > simply use BETWEEN in my SQL string, rather I must feed it each
> > individual period as shown in my example using the IN keyword.
> >
> > I am at a loss on how to do this efficiently, but perhaps a select
> > case or two?
> >
> > Thanks much in advance for your kind assistance.
> >

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocate Value Across Monthly Periods AlanR Microsoft Excel Worksheet Functions 1 26th Oct 2009 12:59 AM
How to subtract/add periods in monthly base? =?Utf-8?B?RXJpYw==?= Microsoft Excel Misc 3 1st Dec 2006 04:06 AM
Calculating monthly periods with non traditional dates... =?Utf-8?B?Sm9lQWNjZXNzMzgx?= Microsoft Access Queries 1 1st Apr 2006 09:49 PM
Number of semi-monthly periods between 2 dates =?Utf-8?B?c2ZvcnI=?= Microsoft Excel Worksheet Functions 15 16th Jun 2005 03:45 AM
Creating 6 monthly periods Dinsdale Microsoft Access VBA Modules 1 1st Apr 2004 07:41 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:10 PM.