| Author |
Topic |
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 06:09:33
|
Hello,I have a string of values of numbers in a table.How do i select them in multiple sets of 500, ie declare @set varchar(max), @x intset @x = 1while @x =< 10beginset @set = --- Get the values out in multiples of 500 or any other numberPrint 'This is set '+convert(@x as varchar(2))+' : '+@setset @x = @x + 1end _____________________Yes O ! |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 06:18:07
|
| [code] declare @x int SELECT @x= CASE WHEN (COUNT(*)%500)=0 THEN (COUNT(*)/500) ELSE (COUNT(*)/500) + 1 END FROM YourTable;With CTE (Seq,Numberfield) AS(SELECT ROW_NUMBER() OVER(ORDER BY NumberField) AS Seq,NumberField FROM YourTable)WHILE @x >0BEGINSELECT * FROM CTE WHERE Seq BETWEEN ((@x -1)*500) + 1 AND @x*500SET @x=@x-1 END[/code] |
 |
|
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 06:23:42
|
| Sorry,what is Numberfield, the column i want to retrieve ? and what is seq and cte ?_____________________Yes O ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 07:01:13
|
quote: Originally posted by mary_itohan Sorry,what is Numberfield, the column i want to retrieve ? and what is seq and cte ?_____________________Yes O !
numberfield is column of table which has numbers which you want to retrieve as multiples of 500seq is a rownumber which is dynamically done to group records as per multiples of 500cte is common table expressions. it just used to generate the unique numbered field |
 |
|
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 08:18:37
|
| sorry sir, but there is an errorMsg 156, Level 15, State 1, Line 11Incorrect syntax near the keyword 'WHILE'._____________________Yes O ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 08:22:05
|
ok then try like this:-declare @x int SELECT @x= CASE WHEN (COUNT(*)%500)=0 THEN (COUNT(*)/500) ELSE (COUNT(*)/500) + 1 END FROM YourTableWHILE @x >0BEGINSELECT * FROM YourTable WHERE NumberField BETWEEN ((@x -1)*500) + 1 AND @x*500SET @x=@x-1 END |
 |
|
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 08:27:05
|
I get this errorMsg 248, Level 16, State 1, Line 9The conversion of the varchar value '278312345678' overflowed an int column. Maximum integer value exceeded.So i changed the code in line 9, to convert it to varchar(max) but still getting the errordeclare @x int SELECT @x= CASE WHEN (COUNT(*)%500)=0 THEN (COUNT(*)/500) ELSE (COUNT(*)/500) + 1 END FROM historyWHILE @x >0BEGINSELECT cast(receipient as varchar(max)) FROM historyWHERE receipient BETWEEN ((@x -1)*500) + 1 AND @x*500SET @x=@x-1 END _____________________Yes O ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 08:32:08
|
| is the number field a varchar column? why is it declared varchar if it holds numeric values? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 08:34:22
|
anyways, if its varchar try like belowdeclare @x bigint SELECT @x= CASE WHEN (COUNT(*)%500)=0 THEN (COUNT(*)/500) ELSE (COUNT(*)/500) + 1 END FROM YourTableWHILE @x >0BEGINSELECT receipient FROM YourTable WHERE cast(receipient as bigint) BETWEEN ((@x -1)*500) + 1 AND @x*500SET @x=@x-1 END |
 |
|
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 08:38:05
|
| (0 row(s) affected)am confused :-(_____________________Yes O ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 08:50:51
|
quote: Originally posted by mary_itohan (0 row(s) affected)am confused :-(_____________________Yes O !
ok. show your sample data from table and show how you want output |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 08:58:53
|
Try this also:-CREATE TABLE #Temp(Seq bigint, receipient bigint) declare @x bigint SELECT @x= CASE WHEN (COUNT(*)%500)=0 THEN (COUNT(*)/500) ELSE (COUNT(*)/500) + 1 END FROM YourTableINSERT INTO #Temp (Seq,receipient)SELECT ROW_NUMBER() OVER(ORDER BY cast(receipient as bigint)),cast(receipient as bigint)FROM YourTableWHILE @x >0BEGINSELECT * FROM #Temp WHERE Seq BETWEEN ((@x -1)*500) + 1 AND @x*500SET @x=@x-1 END |
 |
|
|
mary_itohan
Posting Yak Master
191 Posts |
Posted - 2008-10-05 : 09:30:24
|
| Ok great it works.I will integrate the other script your wrote for me into it.thank you_____________________Yes O ! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-05 : 09:48:45
|
quote: Originally posted by mary_itohan Ok great it works.I will integrate the other script your wrote for me into it.thank you_____________________Yes O !
welcome Dont hesitate to ask if you've any more doubt |
 |
|
|
|