Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
TSQL padleft
Message
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Title:
Environment versions
SQL Server:
SQL Server 2005
Miscellaneous
Thread ID:
01387814
Message ID:
01408975
Views:
53
Thanks, William.

>>I need to padlef a string with zeros in TSQL, but I can't find the command.
>>42 -> 0042
>>
>>Any help would be appreciated.
>>
>>Thanks,
>>Einar
>
>For reference, here are TSQL function definitions for PADL and PADR..
>
>_______________________________________________
>
>create function PADL (@pcString varchar(200),@pnLength int,@pcPadChar char(1))
>returns varchar(200)
>
>as
>
>BEGIN
>
>declare @ReturnString varchar(200)
>
>if @pcString is NULL
> set @pcString = ''
>
>if datalength(@pcString)>@pnLength
>
> set @ReturnString = left(@pcString,@pnLength)
>
>else
>
> set @ReturnString = (SELECT REPLICATE(@pcPadChar, @pnLength - DATALENGTH(@pcString)) + @pcString)
>
>
>return @ReturnString
>
>END
>
>______________________________________________________
>
>
>create function PADR (@pcString varchar(200),@pnLength int,@pcPadChar char(1))
>returns varchar(200)
>
>as
>
>BEGIN
>
>declare @ReturnString varchar(200)
>
>if @pcString is NULL
> set @pcString = ''
>
>if datalength(@pcString) > @pnLength
>
> set @returnString = left(@pcString,@pnLength)
>
>else
>
> set @ReturnString = (SELECT @pcString + Replicate(@pcPadChar,@pnLength - datalength(@pcString)) )
>
>
>return @ReturnString
>
>END
If it's not broken, fix it until it is.


My Blog
Previous
Reply
Map
View

Click here to load this message in the networking platform