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 |
|
tawnie
Starting Member
2 Posts |
Posted - 2009-12-23 : 01:59:59
|
| Hi,I would like to have a syntax to convert the following data into rows. Ok i have columns as below.Doc Batch1 1-52 6-10i want an output as belowDoc Batch1 11 2 1 31 41 52 62 72 82 92 10please assist me with a code to give me the above outputRegardstawnie |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-12-23 : 02:35:48
|
| [code]DECLARE @t TABLE (doc INT, batch VARCHAR(32))INSERT INTO @t SELECT 1,'1-5'INSERT INTO @t SELECT 2,'6-10'INSERT INTO @t SELECT 3,'15-17'SELECT t.*,LEFT(batch,CHARINDEX('-',batch)-1)+numberFROM @t tINNER JOIN master..spt_Values m ON m.type = 'p'WHERE LEFT(batch,CHARINDEX('-',batch)-1)+number <= RIGHT(batch,LEN(batch)-CHARINDEX('-',batch))[/code] |
 |
|
|
tawnie
Starting Member
2 Posts |
Posted - 2009-12-23 : 06:08:02
|
| hi i did my query as below;SELECT OWTR.DocNum,left(IBT1.BatchNum,charindex('-',IBT1.BatchNum)-1) as Batch1, RIGHT(IBT1.BatchNum,LEN(IBT1.BatchNum)-CHARINDEX('-',IBT1.BatchNum)) as Batch2FROM OWTR INNER JOIN WTR1 ON OWTR.DocEntry = WTR1.DocEntry INNER JOIN IBT1 ON WTR1.DocEntry = IBT1.BaseEntryWHERE (IBT1.BaseType = 67) AND (IBT1.Direction = '1') i got 3 columnsDocNum Batch1 Batch21 1 52 6 10now my aim is to return the below outputDocNum Batch1 Batch2 batch1 1 5 11 1 5 21 1 5 31 1 5 41 1 5 52 6 10 62 6 10 72 6 10 82 6 10 9 2 6 10 10please let me know how to handle this on my codetawnie |
 |
|
|
ddramireddy
Yak Posting Veteran
81 Posts |
Posted - 2009-12-23 : 06:30:05
|
| declare @t table( Id int identity(1,1), Batch1 int, Batch2 int )insert into @tselect 1,5 union allselect 6,10;with cte as( select ID,Batch1,Batch2,Batch1 as batch from @t union all select c.ID,c.Batch1,c.Batch2,batch + 1 from cte c inner join @t t on c.Id= t.Id and batch < c.Batch2)select * from cte order by Id |
 |
|
|
bklr
Master Smack Fu Yak Hacker
1693 Posts |
Posted - 2009-12-23 : 06:33:56
|
| DECLARE @t TABLE (doc INT, batch1 int, batch2 int)INSERT INTO @t SELECT 1,1,5INSERT INTO @t SELECT 2,6,10INSERT INTO @t SELECT 3,15,17SELECT t.*,batch1+numberFROM @t tINNER JOIN master..spt_Values m ON m.type = 'p'WHERE batch1+number <= batch2 |
 |
|
|
ddramireddy
Yak Posting Veteran
81 Posts |
Posted - 2009-12-23 : 06:43:44
|
quote: Originally posted by bklr DECLARE @t TABLE (doc INT, batch1 int, batch2 int)INSERT INTO @t SELECT 1,1,5INSERT INTO @t SELECT 2,6,10INSERT INTO @t SELECT 3,15,17SELECT t.*,batch1+numberFROM @t tINNER JOIN master..spt_Values m ON m.type = 'p'WHERE batch1+number <= batch2
More better approach than me..... Hopefully, Batch Duration will be less than 2048... :)and In my case also hopefullt the batch duration will be less than 32,768 |
 |
|
|
|
|
|
|
|