Skip to content

Instantly share code, notes, and snippets.

@vikaskanani
Last active January 31, 2019 06:39
Show Gist options
  • Save vikaskanani/7834530ee4428fe627273cc8dacb8776 to your computer and use it in GitHub Desktop.
Save vikaskanani/7834530ee4428fe627273cc8dacb8776 to your computer and use it in GitHub Desktop.
SQL function that removes the leading zeros from string
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Vikas Patel
-- Create date: 01/31/2019
-- Description: Remove leading zeros from string
-- =============================================
CREATE FUNCTION dbo.funRemoveLeadingZeros
(
-- Add the parameters for the function here
@Input varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
-- Declare the return variable here
DECLARE @Result varchar(max)
-- Add the T-SQL statements to compute the return value here
SET @Result = @Input
WHILE LEFT(@Result, 1) = '0'
BEGIN
SET @Result = SUBSTRING(@Result, 2, LEN(@Result) - 1)
END
-- Return the result of the function
RETURN @Result
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment