Skip to main content

SQL Server function to trim leading zeros.

-- =============================================
-- Author:      Jon LaBelle
-- Create date: November 13, 2023
-- Description: Trims leading zeros from a string.
-- =============================================

create function TrimLeadingZeros(
    @str varchar(255)
)
returns varchar(255)
as
begin
    if @str is null
    begin
        return null;
    end;

    return replace(ltrim(replace(@str, '0', ' ')), ' ', '0');
end;