TSQL - Removing leading zeros from varchar value:
How to remove leading zeros from a number using TSQL
ALTER FUNCTION [dbo].[RemoveLeadingZero] (@StrValue NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
BEGIN
SET @StrValue = ISNULL(@StrValue,'')
SET @StrValue = SUBSTRING(@StrValue, PATINDEX('%[^0]%', @StrValue+'.'), LEN(@StrValue))
Return @StrValue
END
Result:
SELECT DBO.RemoveLeadingZero('00050407')
50407
SELECT DBO.RemoveLeadingZero('00050470')
50470
SELECT DBO.RemoveLeadingZero('000.5430')
.5430
No comments:
Post a Comment