Yesterday, my colleague asked me if SQL Server has any such information.. NET Framework. padleft, String. the padright method does not have the correct internal letter number, but there is a similar replicate string number, it is used to realize the character filling function in the SQL Server String.
The sampling method is not recommended. A new parameter number is added, which includes three parameters: original string-@ source, fill in character-@ paddingchar, return String Length-@ totalwidth, please refer to the following program example:
CREATE FUNCTION [dbo].[PadLeft] |
@Source nvarchar(30), -- Original string |
@PaddingChar nchar (1), -- Fill in the delimiter |
@TotalWidth tinyint -- Overall length |
DECLARE @Result nvarchar(285) |
SELECT @Result = REPLICATE(@PaddingChar, @TotalWidth - LEN(@Source)) + @Source |
The replicate statement is based on the number of string functions created in SQL Server. The replicate statement is used to specify the number of unique characters (the total length-the length of the original string ), in addition, the original string is used to complete the left-side padding function. Similarly, if you enter "Replicate" in the "right" field, the original string is placed before the replicate function.
The usage is also very simple. After adding padleft and padright In the northwind data warehouse, check the following rows:
04 |
SELECT TOP 5 dbo.PadLeft([ProductID], '0' , 8) AS [SeqNo], ProductID, ProductName, QuantityPerUnit, UnitPrice |
08 |
SELECT TOP 5 dbo.PadRight([ProductID], '0' , 8) AS [SeqNo], ProductID, ProductName, QuantityPerUnit, UnitPrice |
For the effect, set the productid score to 0 on the left and right to 8 sequence values:
Reference to: http://www.dotblogs.com.tw/hunterpo/archive/2009/08/18/10142.aspx