Looking for something specific?
  Home
Home
Articles
Page Tag-Cloud
  Software
Software Tag-Cloud
Building from Source
Open Source Definition
All Software
  Popular Tags
Legacy
C Plus Plus
Source Code
Class
Cryptography
  Members
Login
Web-Email
Notable Members
  Official
Our Company
Copyright Information
Software EULA
GPL EULA
LGPL Eula
Pre-Release EULA
Privacy Policy
  Support
Make Contact
 
 
String and Numeric Padding
Have you ever ran into one of those cases when you needed to fix the length of a value? Well, it turns out to be a pretty straight forward task. These functions can be easily adapted to pad some other value as well.

CREATE FUNCTION PadNumeric
(
	@Numeric Numeric,
	@PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
	RETURN Replace(Space(@PadTo - Len(Convert(Varchar, @Numeric))), ' ', 0) + Convert(Varchar, @Numeric)
END
CREATE FUNCTION [dbo].[PadLeft]
(
    @String VarChar(50),
    @PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
    RETURN Replace(Space(@PadTo - Len(@String)), ' ', 0) + @String
END
CREATE FUNCTION [dbo].[PadRight]
(
	@String VarChar(50),
	@PadTo Int = 10
)
RETURNS VarChar(50)
AS
BEGIN
	RETURN @String + Replace(Space(@PadTo - Len(@String)), ' ', 0)
END


Is this code snippet, product or advice warrantied against ill-effect and/or technical malaise? No. No it's not! Not expressed - Not implied - not at all.



Tags:
 Function    Snippet    SQL Server    Strings  

Created by Josh Patterson on 2/9/2013, last modified by Josh Patterson on 2/9/2013

No comments currently exists for this page. Why don't you add one?
First Previous Next Last 

 
Copyright © 2024 NetworkDLS.
All rights reserved.
 
Privacy Policy | Our Company | Contact