If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Microsoft SQL Server > Can I select multi recs from single recs without cursor?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-10-10, 03:31
shell_l_d shell_l_d is offline
Registered User
 
Join Date: Dec 2009
Posts: 19
Question Can I select multi recs from single recs without cursor?

Wondering if there's a way to do a select query without a cursor (although easy enough to do, curious if can be done without one) to do something like this... here's a small scale sample of what I'm trying to do with a select query...

Sample recs in PublicHols table:
26Jan2010,26Jan2010 -- = 1 day
25Apr2010,25Apr2010 -- = 1 day
25Dec2010,26Dec2010 -- = 2 days

Sample results expected:
26Jan2010,1
25Apr2010,1
25Dec2010,1
26Dec2010,1

Sample results at moment without cursor:
26Jan2010,1
25Apr2010,1
25Dec2010,2 -- want this split into 2 records instead

At moment I'm using this but it can return days > 1, so considering cursor to select them broken up into single records:
select
StartDate,
cast(EndDate-StartDate as integer)+1 as Days
from PublicHols
Reply With Quote
  #2 (permalink)  
Old 09-10-10, 10:37
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
select dateadd(dd,n.number,h.StartDate)
from master..spt_values n
join PublicHols h
  on n.type='P' 
 and dateadd(dd,n.number,StartDate) between h.StartDate and h.EndDate
Reply With Quote
  #3 (permalink)  
Old 09-11-10, 01:40
shell_l_d shell_l_d is offline
Registered User
 
Join Date: Dec 2009
Posts: 19
Talking

Thanks heaps pdreyer, that's awesome.

Didn't even know about master..spt_values looks like it will come in handy for other stuff too, cool taa
__________________
SQL Server 2000 (& 2005)

Last edited by shell_l_d; 09-11-10 at 01:49.
Reply With Quote
  #4 (permalink)  
Old 09-13-10, 04:00
Wim Wim is offline
Registered User
 
Join Date: Nov 2004
Posts: 1,279
Quote:
Originally Posted by pdreyer View Post
Code:
select dateadd(dd,n.number,h.StartDate)
from master..spt_values n
join PublicHols h
  on n.type='P' 
 and dateadd(dd,n.number,StartDate) between h.StartDate and h.EndDate
Can you point me to a page that explains the values in spt_values? I did a SELECT * from spt_values, but I don't know what all those values stand for.

When Googleing for it, I found many places where it is used (or bashed) but I haven't been able to find a description of it. Nor can I find it in any MSSQL book I have.
__________________
With kind regards . . . . . SQL Server 2000/2005/2008/2008 R2 Earned beers: 16
Wim
Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald Knuth
Grabel's Law: 2 is not equal to 3 -- not even for very large values of 2.
Pat Phelan's Law: 2 very definitely CAN equal 3 -- in at least two programming languages
Reply With Quote
  #5 (permalink)  
Old 09-13-10, 05:09
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
It is a system table for internal use. The only application I have ever seen is by developers as an auxilary numbers table. Personally I prefer a bespoke, user defined numbers table but it is useful when answering questions on forums.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #6 (permalink)  
Old 09-13-10, 05:13
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
In fact, as an example of the information it stores look at:
Code:
SELECT  name
      , number
FROM    master.dbo.spt_values
WHERE   type = 'A'
and compare to the options in the BoL entry for sp_serveroption
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
Reply

Tags
cursor, multiple, select, single

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On