In SQL Server 2000 how do you add {n} number of 0's to a nchar(n)?

↘锁芯ラ 提交于 2019-12-11 07:49:24

问题


I'm using SQL Server 2000 and I'm not sure if there is already a function to add any number of 0's to an nchar(n), or if this is best done in a function?

Does this look correct

  proc repeatingCh
     @n int, -- i/p n to some number of characters
     @ch nchar(1), --i/p character
     @ch_n nchar(n) out
  as
  begin
     declare @ch_n as nchar(n)
     set @ch_n = ''

     for(i=1 to n)
     begin
        @ch_n = @ch_n + @ch
     end

     return 1
  end

I want to call it in a sproc or... most likely a function and return the repeating characters.


回答1:


me again ;) You can use REPLICATE for this:

SELECT REPLICATE('0', 10)



回答2:


ex your record is unsized col1 55 66 777 888

you need all in 6 digit

if select replicate('0',6-len(col1))++col1

or

declare @input varchar(10)

set @input = '1234'

select replicate('0',10-len(@input))++@input



来源:https://stackoverflow.com/questions/5673183/in-sql-server-2000-how-do-you-add-n-number-of-0s-to-a-ncharn

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!