Microsoft SQL Server 2005 T-SQL does not have left/right string padding function, to use we need to write our own Microsoft SQL function. The following code example shows how to create a function for Left string padding
[T-SQL Code Snippet]
SET ANSI_NULLS ON |
SET QUOTED_IDENTIFIER ON |
Go |
CREATE FUNCTION [dbo].[lPad] |
( |
--Input String to Pad |
@in_str AS VARCHAR(MAX) |
--Character used for padding an input string |
,@in_padd_char AS VARCHAR(1) |
--Number of Chracter in resulting string, equals to the number of chracters in input string plus additonal padding chracters |
,@in_total_width AS INT |
) |
RETURNS VARCHAR(MAX) WITH EXECUTE AS CALLER |
AS |
BEGIN |
DECLARE @out_padded_str AS VARCHAR(MAX) |
SET @out_padded_str = ISNULL( REPLICATE(@in_padd_char, @in_total_width - len(@in_str) ), '') + @in_str |
RETURN @out_padded_str |
END |
Usage Example
SELECT DBO.lPad ('12345', '0', 10) |
---------------------------------------------------------- |
Result: 0000012345 |
You are welcome Muhammad Azeem and i am glad that it helps you :)
ReplyDeleteralph lauren outlet
ReplyDeletemichael kors handbags
coach factory outlet
michael kors outlet
toms outlet
michael kors outlet
nike basketball shoes
louis vuitton
adidas nmd
coach factory outlet
rolex submariner
kobe bryant shoes
louis vuitton
coach outlet
coach outlet
air jordan 13
nike trainers
ralph lauren polo
louis vuitton outlet
kobe bryant shoes
louis vuitton outlet
ray ban sunglasses outlet
louis vuitton handbags
toms shoes
jordan 11s
air jordan pas cher
michael kors handbags
true religion
kate spade
michael kors outlet
celine
coach outlet online
nike free run
louis vuitton outlet
michael kors outlet
ralph lauren polo
adidas shoes
louis vuitton handbags
ray-ban sunglasses
oakley sunglasses
20167.19wengdongdong