Storing Leading Zero in SQL

By SP

This is the query about storing leading zero in SQL or adding leading zeroes in select query.

To store value 1 as '001',  We need to keep column datatype as varchar.

Here is a query for your

CREATE PROCEDURE Add_LeadingZeros
@Your_Value INT
AS
BEGIN
INSERT INTO Your_Table(ColumnName) VALUES(RIGHT('000'+ CONVERT(VARCHAR,@Your_Value),3))
END

Also if the field is of type INT, we can make the column in select query which results varchar values with leading zeroes

SELECT RIGHT('000'+ CONVERT(VARCHAR,Column1),3) AS 'New_Column' FROM Table1

Hope you find it useful.

Storing Leading Zero in SQL  (937 Views)