Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server / SQL-Server-2014

A Simple Example of Microsoft SQL Server Pad String

4.50/5 (9 votes)
18 Dec 2015CPOL 11.9K  
A simple example of Microsoft SQL Server Pad String

Introduction

An easy way to Pad String using Microsoft SQL Server.

Using the Code

A sample code snippet is given below:

SQL
//
CREATE FUNCTION [dbo].[FUN_pad_string] 

(
	@p_seq VARCHAR(16),
	@p_pad_with CHAR(1),
	@p_pad_length INT
) 

RETURNS VARCHAR(16) AS

BEGIN 

DECLARE @g_current_seq varchar(16)

SELECT @g_current_seq = ISNULL(REPLICATE(@p_pad_with, @p_pad_length - LEN(ISNULL(@p_seq ,0))), '') + @p_seq

RETURN @g_current_seq

END
//

Conclusion

I hope you get the scenario and this might be helpful to you. Enjoy!

History

  • Saturday, December 19th, 2015: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)