0

I need to remove leading characters with variable zeros in my column. The string always starts with '049' + filling zeros + some number. I need to extract the number after leading zeros.

04912040 -> 12040
04901204 -> 1204
04900100 -> 100
04900012 -> 12
04900008 -> 8

I have found this solution and added replace for the leading '049' to be replaced with '000':

SUBSTRING(mycolumn, PATINDEX('%[^0]%', REPLACE(mycolumn, '049', '000')), LEN(mycolumn)) 

However, this won't work if my string looks like 04904901, since instead of 4901 I will get 1.

user2463808
  • 179
  • 11
  • How, pray tell, does the title relate to the question? Is that what you searched for? – HABO Nov 16 '20 at 15:39

1 Answers1

4

Just remove the first 3 characters and convert it to an int:

SELECT CONVERT(int,STUFF(YourColumn,1,3,''))
FROM dbo.YourTable;

db<>fiddle

Thom A
  • 88,727
  • 11
  • 45
  • 75