SQL server 2000 - any gurus?
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- SQL server 2000 - any gurus?
SQL server 2000 - any gurus?
29-03-2017 4:09 PM - edited 29-03-2017 4:25 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
i have a query running on SQL server 2000
declare @selecteddate datetime set @selecteddate = '13 March 2017' select diary_engineer_id, diary_start_date, diary_end_date from diary with (nolock) where diary_link_to_nonprod in ('HO','LD') and (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date) and diary_engineer_id in ('C000021','GB','GC','JA','JMCF','KYLEM') order by diary_engineer_id, diary_end_date
which produces the result
C000021 2017-03-13 00:00:00.000 2017-03-17 00:00:00.000 GB 2017-03-13 00:00:00.000 2017-03-13 00:00:00.000 GC 2017-03-13 00:00:00.000 2017-03-13 00:00:00.000 JA 2017-03-13 00:00:00.000 2017-03-17 00:00:00.000 JMCF 2017-03-13 00:00:00.000 2017-03-13 00:00:00.000 KYLEM 2017-03-13 00:00:00.000 2017-03-13 00:00:00.000
what i need it to do is produce a row for each of the dates within the daterange in each resulting row.
so essentially in this example i would want it to produce
C000021 2017-03-13 00:00 2017-03-13 00:00 C000021 2017-03-14 00:00 2017-03-14 00:00 C000021 2017-03-15 00:00 2017-03-15 00:00 C000021 2017-03-16 00:00 2017-03-16 00:00 C000021 2017-03-17 00:00 2017-03-17 00:00 GB 2017-03-13 00:00 2017-03-13 00:00 GC 2017-03-13 00:00 2017-03-13 00:00 JA 2017-03-13 00:00 2017-03-13 00:00 JA 2017-03-14 00:00 2017-03-14 00:00 JA 2017-03-15 00:00 2017-03-15 00:00 JA 2017-03-16 00:00 2017-03-16 00:00 JA 2017-03-17 00:00 2017-03-17 00:00 JMCF 2017-03-13 00:00 2017-03-13 00:00 KYLEM 2017-03-13 00:00 2017-03-13 00:00
is this possible?
Re: SQL server 2000 - any gurus?
30-03-2017 11:19 AM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Very possible,
several solutions here:
http://stackoverflow.com/questions/17529860/how-to-list-all-dates-between-two-dates
Re: SQL server 2000 - any gurus?
30-03-2017 12:14 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
as i am only passing 1 date into the query, not two.
i am not looking for the dates between two date variables.
Re: SQL server 2000 - any gurus?
30-03-2017 12:21 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
I would set up a calendar with all the possible dates and then cross join the results "where calendar.date between diary_start_date and diary_end_date"
jelv (a.k.a Spoon Whittler) Why I have left Plusnet (warning: long post!) Broadband: Andrews & Arnold Home::1 (FTTC 80/20) Line rental: Pulse 8 Home Line Rental (£14.40/month) Mobile: iD mobile (£4/month) |

Re: SQL server 2000 - any gurus?
30-03-2017 12:26 PM - edited 30-03-2017 12:26 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@chenks76 - What happens when you change the order of the predicate, from :
and (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date)
to something like :
and (diary_end_date <= @selecteddate) and (diary_start_date >= @selecteddate)
Re: SQL server 2000 - any gurus?
30-03-2017 12:28 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Incidently, why not instead of
and (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date)
did you not use
and (@selecteddate between diary_start_date and diary_end_date)
which is much easier to read?
jelv (a.k.a Spoon Whittler) Why I have left Plusnet (warning: long post!) Broadband: Andrews & Arnold Home::1 (FTTC 80/20) Line rental: Pulse 8 Home Line Rental (£14.40/month) Mobile: iD mobile (£4/month) |
Re: SQL server 2000 - any gurus?
30-03-2017 12:30 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@Anonymous That's still only going to return one row for each row in diary.
jelv (a.k.a Spoon Whittler) Why I have left Plusnet (warning: long post!) Broadband: Andrews & Arnold Home::1 (FTTC 80/20) Line rental: Pulse 8 Home Line Rental (£14.40/month) Mobile: iD mobile (£4/month) |
Re: SQL server 2000 - any gurus?
30-03-2017 12:31 PM - edited 30-03-2017 12:34 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@Anonymous wrote:
@chenks76 - What happens when you change the order of the predicate, from :
and (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date)to something like :
and (diary_end_date <= @selecteddate) and (diary_start_date >= @selecteddate)
that produces 2 less records, which would be a wrong outcome.
and doesn't give the desired multiple rows anyway.
Re: SQL server 2000 - any gurus?
30-03-2017 12:32 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@jelv wrote:
Incidently, why not instead of
and (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date)did you not use
and (@selecteddate between diary_start_date and diary_end_date)which is much easier to read?
that produces the same results for the test sample. but the ease of the readability is not my primary concern at the moment
Re: SQL server 2000 - any gurus?
30-03-2017 12:33 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@jelv wrote:
I would set up a calendar with all the possible dates and then cross join the results "where calendar.date between diary_start_date and diary_end_date"
will that not add an extra overhead on the run time of the script?
the supplied query is just a small test sample, but in theory it could include many rows.
Re: SQL server 2000 - any gurus?
30-03-2017 12:37 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
True but you could get the diary start date and end date into variables as a first step and then use those dates to generate the data you want.
something like this;
select @diary_start_date_var = diary_start_date, @diary_end_date_var = diary_end_date from diary where (@selecteddate <= diary_end_date) and (@selecteddate >= diary_start_date)
Re: SQL server 2000 - any gurus?
30-03-2017 12:45 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
only the original varibiale called "selecteddate" is passed into the query (which comes from elsewhere).
only diary_start_Date will always be the same (from the original variable)
diary_end_date could be anything.

Re: SQL server 2000 - any gurus?
30-03-2017 12:53 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
Exactly @chenks76 so it proves that the ordering within the predicate is important. You can now work the rest out knowing that.
Re: SQL server 2000 - any gurus?
30-03-2017 12:55 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
The overheads of a calendar table would be minimal and you'll have the dates as the primary key so the join will be quick.
jelv (a.k.a Spoon Whittler) Why I have left Plusnet (warning: long post!) Broadband: Andrews & Arnold Home::1 (FTTC 80/20) Line rental: Pulse 8 Home Line Rental (£14.40/month) Mobile: iD mobile (£4/month) |
Re: SQL server 2000 - any gurus?
30-03-2017 1:03 PM
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Report to Moderator
@Anonymous wrote:
Exactly @chenks76 so it proves that the ordering within the predicate is important. You can now work the rest out knowing that.
i'm not sure i follow your thought process there.
my original ordering gives the correct outcome, your amended one gives the wrong outcome.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Plusnet Community
- :
- Forum
- :
- Other forums
- :
- Tech Help - Software/Hardware etc
- :
- SQL server 2000 - any gurus?