Quantcast
Viewing latest article 1
Browse Latest Browse All 17

Pad a Number with Zeros – SQL Server 2005 and SQL Server 2008

If you need to pad a number with zeros do the following:

  1. Convert the number to a string padded with spaces by using the STR function.
  2. Replace the spaces with 0’s.  
DECLARE @NumberToPad INT;    SET @NumberToPad = 123    –The number to pad
DECLARE @PadLength INT;    SET @PadLength = 10        –The length of the pad
 
–Pad the string
SELECT REPLACE(STR(@NumberToPad, @PadLength), ‘ ‘, ’0′)

—-

SQL Server Expert, Steve Abraham, holds 8 Microsoft certifications and specializes in SQL Server and .Net Framework architecture, high availability, capacity planning, development, and performance tuning, and SQL Server Recovery.


Filed under: Programming SQL Server, SQL Server 2005, SQL Server 2008, T-SQL Tagged: Number Padding, Pad with Zero, SQL Server, SQL Server 2005, SQL Server 2008, SQL Server Expert, SQL Server Recovery Image may be NSFW.
Clik here to view.
Image may be NSFW.
Clik here to view.

Viewing latest article 1
Browse Latest Browse All 17

Trending Articles