Skip to content Skip to sidebar Skip to footer

T-sql Remove Zero Padding As String Manipulation

If you are dealing with strings as nvarchar and varchar in SQL Server what is the correct way to drop leading zeros without casting to an INT type? Say '000123' for example. I woul

Solution 1:

DECLARE@VarVARCHAR(100) ='000000658410065446'SELECTSUBSTRING(@Var, PATINDEX('%[^0]%',@Var), 100)

OR

SELECTSUBSTRING(@Var, PATINDEX('%[^0]%',@Var), 
                   LEN(@Var)- PATINDEX('%[^0]%',@Var)+ 1)

Both will return the same Result as follows

Result

658410065446

Post a Comment for "T-sql Remove Zero Padding As String Manipulation"