3
For generating repeated characters one function named REPLICATE is available in SQL Server , below is the code
- Declare @Fnum nvarchar(5)
- SET @Fnum= REPLICATE('0',5)
- PRINT @Fnum
3
Hi Schleid Alex,
You can do your mentioned scenarion in following 3 ways:
(1) declare @Fnum nvarchar(5)=''
- Set '' at declaration time.
(2) while (len(isnull(@Fnum, '')) < 5)
- check null at length check time.
- First approach is better than this one.
(3) select right('00000' + cast(isnull(@Fnum, '') as nvarchar(5)), 5)
- You can do padding by this way instead of while loop.
- This approach is better than above both way.
Note
- For all above approaches you must have to use nvarchar instead of nchar
Hope, this will help you!
2
Problem is with nchar make it and cast it to nvarchar, also intialize it, otherwise it will be null.
Also, you are working with string so use CONCAT
- Declare @Fnum nvarchar(5)=''
-
- While (len(@Fnum))<5
- Begin
- SET @Fnum=CAST(CONCAT(@Fnum,'0') AS nvarchar(5))
-
- End
- PRINT @Fnum
0
declare @Fnum as nvarchar() and set default value empty.
Declare @Fnum nvarchar(5) = ''