-4

How can I remove leading zeros from a string such as '0097619896'?

Alex G
  • 595
  • 6
  • 21
  • `CONVERT` it to an `int`. – Thom A Dec 06 '19 at 14:11
  • Possible duplicate of [Better techniques for trimming leading zeros in SQL Server?](https://stackoverflow.com/questions/662383/better-techniques-for-trimming-leading-zeros-in-sql-server) – rory.ap Dec 06 '19 at 14:15

1 Answers1

2

Numerical data types don't contain leading zeroes, so you can convert to a int/bigint/decimal/etc and it'll strip them off:

SELECT CONVERT(int,'0097619896'), CONVERT(decimal(10,0),'0097619896');

If the values aren't numerical values, you can use PATINDEX to find the first non-zero character and STUFF to remove them:

SELECT STUFF(V.YourString,1,PATINDEX('%[^0]%',V.YourString)-1,'')
FROM (VALUES('0097619896-abc'))V(YourString);
Thom A
  • 88,727
  • 11
  • 45
  • 75