If the field is already a string, this will work

 SELECT RIGHT('000'+ISNULL(field,''),3)

If you want nulls to show as '000'

It might be an integer -- then you would want

 SELECT RIGHT('000'+CAST(field AS VARCHAR(3)),3)

As required by the question this answer only works if the length <= 3, if you want something larger you need to change the string constant and the two integer constants to the width needed. eg '0000' and VARCHAR(4)),4

Answer from Hogan on Stack Overflow
๐ŸŒ
Database Guide
database.guide โ€บ left-padding-in-sql-server-3-lpad-equivalents
Left Padding in SQL Server โ€“ 3 LPAD() Equivalents
May 17, 2018 - If you need to apply leading zeros ... returns the number as a string in our specified format: ... In this case, we use the 0 format specifier to format the number with leading zeros where applicable....
Discussions

string - Integer PadLeft function in T-SQL - Stack Overflow
Available since SQL Server 2012 : msdn.microsoft.com/en-us/library/hh213505.aspx 2016-07-15T12:15:46.477Z+00:00 ... Where 4 is desired length. Works for numbers with more than 4 digits, returns empty string on NULL value. ... If someone is still interested, I found this article on DATABASE.GUIDE: Left Padding ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
how to add leading zeroes before and after decimal in sql
Hi , Can somebody help me in writing the query to pad zeros for a decimal values. At the moment it is with 00000.0000000000 ( with a length of 5.10 ) I want to write a statement to convert them to 0000000000.00000000000000000000 ( with a length ofโ€ฆ More on learn.microsoft.com
๐ŸŒ learn.microsoft.com
4
0
sql server - Most efficient T-SQL way to pad a varchar on the left to a certain length? - Stack Overflow
You seem to be talking about left padding numbers such that 1 becomes 001. 2010-12-08T17:12:33.827Z+00:00 ... In SQL Server 2005 and later you could create a CLR function to do this. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Left Padding in TSQL
Does anyone know of a non-kludgy way in TSQL to left-pad zeros onto a numeric field to export into an SDF file? For example, 45000 would show in the SDF file as 00045000 and 1500 would show as 00001500. I am making the switch from FoxPro 2.6 to TSQL. In FoxPro, I had the PADL function that... More on tek-tips.com
๐ŸŒ tek-tips.com
3
0
May 22, 2008
๐ŸŒ
Kodyaz
kodyaz.com โ€บ articles โ€บ sql-pad-leading-zeros.aspx
SQL Tutorial - SQL Pad Leading Zeros - Kodyaz.com
These functions are udfLeftSQLPadding and udfRightSQLPadding for sql padding from left or start of the string and sql padding from the right or end of the string. DECLARE @sqlVariable nvarchar(15) SET @sqlVariable = '1234567890' SELECT @sqlVariable, REPLICATE('0', 15), '000000000000000' + @sqlVariable, RIGHT('000000000000000' + @sqlVariable, 13) SELECT STUFF(@sqlVariable, 1, 0, REPLICATE('0', 15 - LEN(@sqlVariable))) SELECT RIGHT(REPLICATE('0', 15) + LTRIM(RTRIM(@sqlVariable)), 15) SELECT STR(@sqlVariable,15), REPLACE(STR(@sqlVariable,15), SPACE(1), '0') Code ยท
๐ŸŒ
Medium
stevesohcot.medium.com โ€บ how-to-pad-data-with-leading-zeros-in-sql-74fe69b94286
How to pad data with leading zeros in SQL - Steve Sohcot - Medium
March 14, 2024 - SELECT employeeId, RIGHT(REPLICATE('0', 8) + CAST(employeeId AS VARCHAR(8)), 8) AS employeeIdPadded FROM dbo.Employees
๐ŸŒ
Jimsalasek
jimsalasek.com โ€บ 2020 โ€บ 06 โ€บ 02 โ€บ zero-pad-a-string
Zero pad a string โ€“ Jim Salasek's SQL Server Blog
April 12, 2020 - If you have ever used Oracle and needed to create a string padded with zeroโ€™s, they have a nifty function called LPAD which allows you to do this easily. In SQL Server we need to create our โ€ฆ
๐ŸŒ
iheavy
iheavy.com โ€บ home โ€บ exploring sql pad left 0: techniques in sql server
Exploring SQL Pad Left 0: Techniques in SQL Server - iheavy - Devops + Cloud Solutions Architect
May 3, 2025 - By utilizing functions such as FORMAT(), RIGHT(), REPLICATE(), and their combinations, you can left pad zeros effectively within SQL Server environments.
๐ŸŒ
Snowflake Documentation
docs.snowflake.com โ€บ en โ€บ sql-reference โ€บ functions โ€บ lpad
LPAD | Snowflake Documentation
CREATE OR REPLACE TABLE padding_example ... +----------------+------------------------------+ ... This example demonstrates left-padding of VARCHAR values using the LPAD function, with the results limited to 10 characters:...
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ answers โ€บ questions โ€บ 161479 โ€บ how-to-add-leading-zeroes-before-and-after-decimal
how to add leading zeroes before and after decimal in sql - Microsoft Q&A
declare @myval DECIMAL(15,10) set @myval = '02345.0000123245' ;with cte1 as(select format(@myval,'0000000000.0000000000') as col1) ,cte2 as (select col1,right(col1,len(col1)-charindex('.',col1 )) col2 from cte1) select left(col1,charindex('.',col1 ))+left('00000000000000000000',20-len(col2))+col2 from cte2
๐ŸŒ
W3Schools
w3schools.com โ€บ sql โ€บ func_mysql_lpad.asp
MySQL LPAD() Function
String Functions: ASCII CHAR_LENGTH CHARACTER_LENGTH CONCAT CONCAT_WS FIELD FIND_IN_SET FORMAT INSERT INSTR LCASE LEFT LENGTH LOCATE LOWER LPAD LTRIM MID POSITION REPEAT REPLACE REVERSE RIGHT RPAD RTRIM SPACE STRCMP SUBSTR SUBSTRING SUBSTRING_INDEX TRIM UCASE UPPER Numeric Functions: ABS ACOS ASIN ATAN ATAN2 AVG CEIL CEILING COS COT COUNT DEGREES DIV EXP FLOOR GREATEST LEAST LN LOG LOG10 LOG2 MAX MIN MOD PI POW POWER RADIANS RAND ROUND SIGN SIN SQRT SUM TAN TRUNCATE Date Functions: ADDDATE ADDTIME CURDATE CURRENT_DATE CURRENT_TIME CURRENT_TIMESTAMP CURTIME DATE DATEDIFF DATE_ADD DATE_FORMAT DA
๐ŸŒ
Tek-Tips
tek-tips.com โ€บ home โ€บ forums โ€บ software โ€บ programmers โ€บ dbms packages โ€บ microsoft sql server: programming
Left Padding in TSQL | Tek-Tips
May 22, 2008 - The following code will pad an INT to create a string of 10 characters: LEFT('0000000000' + CONVERT(VARCHAR(10), YourIntColumn), 10) ... This code alows for any legth.. I you are using ints then you can convert as in RiverGuys's code ... declare @var as varchar(10) declare @len as int set @var = '45000' set @len = 8 select right(Replicate('0',@len - Len(@var)) + @var,@len) ... Not open for further replies. ... Copy folder and content using SQL Server TSQL.
๐ŸŒ
SQL Authority
blog.sqlauthority.com โ€บ home โ€บ sql server โ€“ pad ride side of number with 0 โ€“ fixed width number display
SQL SERVER - Pad Ride Side of Number with 0 - Fixed Width Number Display - SQL Authority with Pinal Dave
March 27, 2019 - Select A.* ,NewField = B.String From Sample A Cross Apply ( Select String = ltrim((Select cast(RetVal as varchar(25)) From (Select RetSeq,RetVal=Right(โ€™00โ€™+RetVal,2) From [dbo].[udf-Str-Parse](replace(Field,โ€™ โ€˜,โ€™.โ€™),โ€™.โ€™) Where Try_Convert(int,RetVal)>=0 ) A For XML Path (โ€))) ) BReply ... Thank you Pinal Dave for always having such great tutorials. This site has helped me so many times.Reply ... Pinal Dave is an SQL Server Performance Tuning Expert and independent consultant with over 22 years of hands-on experience.
๐ŸŒ
Database Guide
database.guide โ€บ padding-in-sql
Padding in SQL
November 1, 2021 - Oracle also has a TO_CHAR(number) ... position in the original number. PostgreSQL also has its own LPAD() and RPAD() functions: SELECT LPAD('7', 3, '0') AS "Left Padding", RPAD('7', 3, '0') AS "Right Padding", LPAD(RPAD('7', 3, '0'), 5, '0') AS "Both";...
๐ŸŒ
Tech Recipes
tech-recipes.com โ€บ home โ€บ database โ€บ sql server: how to left pad a number with zeros
SQL Server: How to Left Pad a Number with Zeros - Tech Recipes
June 3, 2013 - In the SQL Server 2008 version query, the first parameter is our โ€˜0โ€™ padding string. In the second parameter we are subtracting the length of salary column from 6 which is our max length. Thus, our expression knows how many 0s are needed to left pad and concatenate the salary column.
๐ŸŒ
SQL Studies
sqlstudies.com โ€บ 2021 โ€บ 07 โ€บ 15 โ€บ padding-a-string-in-sql-server
Padding a string in SQL Server | SQL Studies
July 15, 2021 - Itโ€™s a simple enough function that will pad a string on the left with another string. So changing 1234 to 00001234. Itโ€™s a common enough task when formatting strings. And both DB2 and Oracle provide both lpad and rpad functions. However, guess what? SQL Server doesnโ€™t. So how do we handle that in T-SQL? Itโ€™s a pretty easy pattern. DECLARE @MyPad varchar(9) = '1234'; SELECT RIGHT(REPLICATE('0',9)+@MyPad,9);
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ mysql โ€บ mysql-lpad
MySQL LPAD() Function: Usage & Examples
This example pads the string `'123'` with zeros on the left, resulting in `'00123'`. ... Here, the string `'abc'` is padded with the characters `'xy'` to a length of 6, producing `'xyxabc'`. If the `pad_string` is longer than needed, it will be truncated appropriately. sql SELECT LPAD(account_number, 10, '*') AS padded_account FROM accounts;
๐ŸŒ
SQLServerCentral
sqlservercentral.com โ€บ forums โ€บ topic โ€บ how-do-you-pad-left-like-using-t-sql-query
How do you pad left like using T-SQL query? โ€“ SQLServerCentral Forums
November 17, 2014 - Actually, STR() is pretty slow (more than 2X slower) and uses more than twice the CPU. I won't use it for padding when it's so simple to do otherwise. Here's a link for the performance testing I did a couple of years ago. Look for the "STR() is... SLOW!" section title in the article. http://www.sqlservercentral.com/articles/T-SQL/71565/
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ db2-for-zos โ€บ 12.0.0
LPAD scalar function - Db2 SQL
The LPAD function returns a string that is composed of string-expression that is padded on the left, with pad or blanks. The LPAD function treats leading or trailing blanks in string-expression as significant.