How to Add Leading Zeros in SQL Server 2005
- 1). Click the Windows "Start" button and select "All Programs." Click "Microsoft SQL Server," then click "SQL Server Management Studio."
- 2). Log in to your database, then click the database name in the left panel to activate it. Click "New Query" to open the T-SQL editor.
- 3). Create the variables. You create a variable to contains the new string after you convert the number to a string. Type the following code in the editor:
declare @number as varchar(20) - 4). Convert the number and assign the converted number to the string:
set @number = cast(@original as varchar(20)) - 5). Add leading zeros to the string:
set @number = '000' + @number