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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 query unformatted data

Author  Topic 

byomjan
Starting Member

34 Posts

Posted - 2010-02-18 : 17:43:06
rows of a table

STUDENT101 101
ROLLN101 101
MARKS101 78
CLASS101 A1
END101 101
UNIVERSITY KK
STUDENT105 105
ROLLN105 105
MARKS105 100
CLASS105 A1
END105 105
XXX XXX
SERIAL 1.01



I want to loop repeatitive groups i.e from STUDENT*** Untill it hits it's corresponding END*** with the use of a cursor.

Can you please advise.



Byomjan....

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2010-02-18 : 17:49:27
please post the structure of the table as well as the expected output.

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 00:01:36
quote:
Originally posted by byomjan

rows of a table

STUDENT101 101
ROLLN101 101
MARKS101 78
CLASS101 A1
END101 101
UNIVERSITY KK
STUDENT105 105
ROLLN105 105
MARKS105 100
CLASS105 A1
END105 105
XXX XXX
SERIAL 1.01



I want to loop repeatitive groups i.e from STUDENT*** Untill it hits it's corresponding END*** with the use of a cursor.

Can you please advise.



Byomjan....


why do you need loop? what is it that you're trying to do?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-19 : 00:32:22
why are you storing data like this? Is you attempt to get output like this?


SELECT MAX(CASE WHEN Col LIKE 'STUDENT%' THEN Val ELSE NULL END) AS Student,
MAX(CASE WHEN Col LIKE 'ROLLN%' THEN Val ELSE NULL END) AS RollNo,
MAX(CASE WHEN Col LIKE 'MARKS%' THEN Val ELSE NULL END) AS Marks,
MAX(CASE WHEN Col LIKE 'CLASS%' THEN Val ELSE NULL END) AS Class,
MAX(CASE WHEN Col LIKE 'END%' THEN Val ELSE NULL END) AS [End]
FROM(
SELECT ROW_NUMBER() OVER(PARTITION BY LEFT(Col,PATINDEX('%[0-9]%',Col)-1) ORDER BY SUBSTRING(Col,PATINDEX('%[0-9]%',Col)+1,LEN(Col))*1) AS Seq,Col,Val
FROM
(
SELECT 'STUDENT101' AS Col,'101' AS Val UNION ALL
SELECT 'ROLLN101', '101' UNION ALL
SELECT 'MARKS101', '78' UNION ALL
SELECT 'CLASS101', 'A1' UNION ALL
SELECT 'END101', '101' UNION ALL
SELECT 'UNIVERSITY', 'KK' UNION ALL
SELECT 'STUDENT105', '105' UNION ALL
SELECT 'ROLLN105', '105' UNION ALL
SELECT 'MARKS105', '100' UNION ALL
SELECT 'CLASS105', 'A1' UNION ALL
SELECT 'END105', '105' UNION ALL
SELECT 'XXX', 'XXX' UNION ALL
SELECT 'SERIAL', '1.01'
)t
WHERE Col LIKE '%[0-9]%'
)r
GROUP BY Seq

output
-------------------------------------------
Student RollNo Marks Class End
101 101 78 A1 101
105 105 100 A1 105



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -