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 |
|
karthickbabu
Posting Yak Master
151 Posts |
Posted - 2008-01-03 : 01:31:15
|
| Hi I create a query for number sequence like as below 1,4,9,...100my code as below :DECLARE @Iterator IntSET @Iterator = 1WHILE @Iterator <=10BEGIN --SELECT SQUARE(@Iterator) AS Sequence PRINT SQUARE(@Iterator) SET @Iterator = @Iterator + 1 ENDThe output can be shown as below:149......100Is it possible to shown the output as below1,4,9,...100I tried more things but getting errors only or otherwise it print null valuekindly reply (is it possible?) |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-01-03 : 01:59:48
|
DECLARE @Iterator Int, @output varchar(40)SELECT @Iterator = 1, @output = ''WHILE @Iterator <=10SELECT @output = @output + ',' + convert(varchar(4), SQUARE(@Iterator)),@Iterator = @Iterator + 1 select substring(@output, 2, 40)print substring(@output, 2, 40) E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-03 : 02:01:34
|
Use this:-DECLARE @Iterator Int,@Res varchar(100)SET @Iterator = 1WHILE @Iterator <=10BEGIN--SELECT SQUARE(@Iterator) AS SequenceSELECT @Res=COALESCE(@Res,'') + CAST(SQUARE(@Iterator) AS varchar(5))+ ','SET @Iterator = @Iterator + 1 ENDPRINT LEFT(@Res,LEN(@res)-1) |
 |
|
|
karthickbabu
Posting Yak Master
151 Posts |
Posted - 2008-01-03 : 02:03:46
|
| Thanks for your fastest reply |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-01-03 : 02:10:56
|
orDECLARE @output varchar(40)SELECT @output = ''SELECT @output = @output + ',' + convert(varchar(4), SQUARE(number))from master..spt_valueswhere type='p' and number between 1 and 10select substring(@output, 2, 40)print substring(@output, 2, 40) MadhivananFailing to plan is Planning to fail |
 |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2008-01-03 : 02:12:09
|
select convert(varchar, number*number) + ','from master.dbo.spt_valueswhere type='P' and number between 1 and 10order by number ascfor xml path ('') elsasoft.org |
 |
|
|
|
|
|
|
|