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
 General SQL Server Forums
 New to SQL Server Programming
 REPLICATE

Author  Topic 

pazzy11
Posting Yak Master

145 Posts

Posted - 2007-10-04 : 10:41:48
This code :
[CODE]
declare @filenumber as char
set @filenumber = '65'

declare @filenu2 as char(6)

set @filenu2 = CONVERT(CHAR(6),(replicate('0',(6-len(@filenumber)))+ @filenumber ))

select @filenu2 as x
[/CODE]


This should return 000065
but it returns 000006
i have stepped through it the replicate should replicate the 0 4 times
(6 - len of @filenumber) instaed its doing it 5 ??

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-04 : 10:44:11
quote:
Originally posted by pazzy11

This code :
[CODE]
declare @filenumber as char(2)
set @filenumber = '65'

declare @filenu2 as char(6)

set @filenu2 = CONVERT(CHAR(6),(replicate('0',(6-len(@filenumber)))+ @filenumber ))

select @filenu2 as x
[/CODE]


This should return 000065
but it returns 000006
i have stepped through it the replicate should replicate the 0 4 times
(6 - len of @filenumber) instaed its doing it 5 ??



Madhivanan

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

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-04 : 10:44:52
Also if you use front end application, use format function there

Madhivanan

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

pazzy11
Posting Yak Master

145 Posts

Posted - 2007-10-04 : 10:45:14
what happens if the length of filenumber will vary ?
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2007-10-04 : 10:47:33
Just make it char(25) and be done with it...

or char(50) or char(100)...but in this case char(6) would probably be sufficient.
Go to Top of Page

pazzy11
Posting Yak Master

145 Posts

Posted - 2007-10-04 : 11:02:34
ok thanks ..
.. AGain !!

madhivanan you save me some trouble !!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-05 : 01:56:56
quote:
Originally posted by pazzy11

ok thanks ..
.. AGain !!

madhivanan you save me some trouble !!


Weel. You may need to consider this also


declare @filenumber as int
set @filenumber = 65

select right(replicate('0',6)+ cast(@filenumber as varchar(6)),6) as x




Madhivanan

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

ranganath
Posting Yak Master

209 Posts

Posted - 2007-10-05 : 02:43:10
hi,

Try This also

Declare @x varchar(100)
Set @x = '65'
Select Replicate ('0', (6-Len(@X)))+ @x
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-10-05 : 04:12:11
quote:
Originally posted by ranganath

hi,

Try This also

Declare @x varchar(100)
Set @x = '65'
Select Replicate ('0', (6-Len(@X)))+ @x



Declare @x varchar(100)
Set @x = '6577777'
Select Replicate ('0', (6-Len(@X)))+ @x

Madhivanan

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

- Advertisement -