Last active
January 31, 2019 06:39
-
-
Save vikaskanani/7834530ee4428fe627273cc8dacb8776 to your computer and use it in GitHub Desktop.
SQL function that removes the leading zeros from string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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