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)
 Sequence Output

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,...100

my code as below :

DECLARE @Iterator Int
SET @Iterator = 1
WHILE @Iterator <=10
BEGIN
--SELECT SQUARE(@Iterator) AS Sequence
PRINT SQUARE(@Iterator)
SET @Iterator = @Iterator + 1
END

The output can be shown as below:
1
4
9
..
..
..
100


Is it possible to shown the output as below
1,4,9,...100

I tried more things but getting errors only or otherwise it print null value

kindly 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 <=10
SELECT @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"
Go to Top of Page

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 = 1
WHILE @Iterator <=10
BEGIN
--SELECT SQUARE(@Iterator) AS Sequence
SELECT @Res=COALESCE(@Res,'') + CAST(SQUARE(@Iterator) AS varchar(5))+ ','
SET @Iterator = @Iterator + 1
END
PRINT LEFT(@Res,LEN(@res)-1)
Go to Top of Page

karthickbabu
Posting Yak Master

151 Posts

Posted - 2008-01-03 : 02:03:46
Thanks for your fastest reply



Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-03 : 02:10:56
or


DECLARE @output varchar(40)
SELECT @output = ''

SELECT
@output = @output + ',' + convert(varchar(4), SQUARE(number))
from
master..spt_values
where
type='p' and number between 1 and 10

select substring(@output, 2, 40)
print substring(@output, 2, 40)


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

jezemine
Master Smack Fu Yak Hacker

2886 Posts

Posted - 2008-01-03 : 02:12:09
select convert(varchar, number*number) + ','
from master.dbo.spt_values
where type='P' and number between 1 and 10
order by number asc
for xml path ('')


elsasoft.org
Go to Top of Page
   

- Advertisement -