Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
CaseyC
Starting Member
8 Posts |
Posted - 2002-03-07 : 15:08:28
|
| I am having difficulty finding dates between two datesFor example, I have:@wk15= convert(varchar(10), dateadd(dd, -105, @date), 112) ---105 is fifteen seven day periods from March 1 @wk14= convert(varchar(10), dateadd(dd, -98, @date), 112)And when I use something as simple as this:SELECT @wk15, #Dates.June_March (June_March being a list of all workdays from June 01 to Mar 1)FROM #DatesWHERE #Dates.June_March >= @wk15When I use #Dates.June_March <= @wk15 then it returns all dates. Why can't I just get the dates in between a 1 week period? Any better suggestions.Many thanks in advance!Casey Cullinan |
|
|
setbasedisthetruepath
Used SQL Salesman
992 Posts |
Posted - 2002-03-07 : 15:15:59
|
| It's hard to tell from your example what you're trying to accomplish, exactly, but here are two suggestions that might help:First, don't convert the dates to strings. If you intend to use the comparison operators ( <, >, etc. ) you should leave them as datetime columns. Second, it sounds like the BETWEEN clause might be all you need. Something like:set @march_week_1 = '2002-03-01'set @march_week_2 = '2002-03-08'select *from #dateswhere june_march between @march_week_1 and @march_week_2 |
 |
|
|
Jay99
468 Posts |
Posted - 2002-03-07 : 15:18:55
|
| Not exactly sure what you are trying to do, but have you tried a BETWEEN clause?Is June_March a varchar(10) with the same date format as @wk15?Please post more information.JayEDIT: grrrrr . . . echo offEdited by - Jay99 on 03/07/2002 15:20:40 |
 |
|
|
CaseyC
Starting Member
8 Posts |
Posted - 2002-03-08 : 08:50:30
|
| I probably didn't articulate my problem well enough. I am trying to build a forecasting report that takes past data to show trends. I am trying to find 15 weeks back (expressed in a term of 7 days - not following a typical week or work week) from a given date. For example I want to find 15 seven day periods from March 1. I will, in turn, join all of my products created (create date is a datetime field - no two create dates are alike due to timestamping) to each week. I was trying to 1. make a table of 15 weeks for each first day of each month, so Jan 1 will have 15 7 day periods back and so forth all the way to Dec 1.) I was then going to take all create dates in my tables > 100,000 recs. and summarize product #'s and $'s by week. My problem is that I am having a heck of a time trying to forge these seven day weeks and THEN trying to put create dates between them.I have tried taking the converts off as well as using between statements with no success.I hope this explains things better. I welcome any ideas. Many thanks. |
 |
|
|
Jay99
468 Posts |
Posted - 2002-03-08 : 09:26:44
|
First, here is my test environmentcreate table products ( productid int identity(1,1), createdate datetime)godeclare @counter intset @counter = 1while @counter < 1000begin insert products values (getdate() - 107) set @counter = @counter + 1endgo Now you said given a specified date (I used getdate), you wanted all the products created from 15 weeks (7 days is a typical week, at least in my home town) ago to that specified date, summarized by date. Here is what I did, is this close to what you are looking for?create table #reportingweeks ( weeknumber tinyint, reportdate datetime)declare @counter int, @givendate datetimeset @counter = 1set @givendate = getdate()while @counter <= 15begin insert #reportingweeks values ((16 - @counter), (@givendate - (7 * @counter))) set @counter = @counter + 1endselect productid, createdate, weeknumberfrom products inner join #reportingweeks on datediff(dd, reportdate, dateadd(dd, 15*7, createdate))<= 7go If I am still off, gimme you create table statement and your desired report . . .Jay |
 |
|
|
CaseyC
Starting Member
8 Posts |
Posted - 2002-03-08 : 09:47:49
|
| That was great help, thank you dearly. I do have a question though, how would I modify that to make the weeks as columns (so that I may create a table of fifteen weeks before every first of the month for an entire year), for example:@givendate, @wk1, @wk2, ...@wk15@givendate, @wk1, @wk2, ...@wk15@givendate, @wk1, @wk2, ...@wk15Thank you again, I truly appreciate your help. Oh and here in Iowa a week is seven days too, I didn't mean to sound so blonde. I meant that instead of weeks 1-52, we were just going seven days back from said date.Edited by - CaseyC on 03/08/2002 10:25:21 |
 |
|
|
Jay99
468 Posts |
Posted - 2002-03-08 : 10:28:38
|
Casey . . .i think you have the idea that you will need a seven day date range to join other dates to in order to determine inclusion; you could use datediff( wk, [date1],[date2] ) = 0 and eliminate the headache. Additionaly, unless you are going for some performance tuning effort, there is no need to store these calculated dates.quote: I didn't mean to sound so blonde...
I just typed and deleted about 5 responses to that one . . .  . . . . . .Jay |
 |
|
|
yakoo
Constraint Violating Yak Guru
312 Posts |
Posted - 2002-03-08 : 10:39:32
|
you could modify Jay's code and make the insert statement dynamiccreate table #weeks ( weeknumber tinyint, reportdate datetime)declare @counter int, @startdate datetime, @enddate datetime, @startSQL varchar(255), @datecounter datetime, @sqlStr varchar(8000)select @startdate = cast('01/01/02' as datetime), @enddate = cast('12/31/02' as datetime), @sqlStr = 'INSERT INTO #weeks (givendate, wk1, wk2, wk3, wk4, wk5, wk6, wk7, wk8, wk9, wk10, wk11, wk12, wk13, wk14, wk15) values ('select @datecounter = @startdatewhile @datecounter <= @enddate begin select @counter = 1, @sqlStr = @startSQL + @datecounter while @counter <= 15 begin select @sqlStr = @sqlStr + ', ' + dateadd(wk, @datecounter, -(7 * @counter)) select @counter = @counter + 1 end select @sqlStr = @sqlStr + ')' exec @sqlStr select @datecounter = dateadd(dd, @datecounter, 1)end |
 |
|
|
|
|
|
|
|