I have code in SQL Server to remove characters from variable
DECLARE @textval NVARCHAR(30) SET @textval = 'CHA000382486' SELECT LEFT(SUBSTRING(@textval, PATINDEX('%[0-9.-]%', @textval), 8000), PATINDEX('%[^0-9.-]%', SUBSTRING(@textval, PATINDEX('%[0-9.-]%', @textval), 8000) + 'X') -1) And also different code to remove leading zeros
DECLARE @textval2 NVARCHAR(30) SET @textval2 = '000382486' SELECT SUBSTRING(@textval2, PATINDEX('%[^0]%', @textval2+'.'), LEN(@textval2)) But I want a single piece of code which would be able to remove alphabets and leading zero.
Can anyone help me?
(big)intyou loose your leading zero's (of course after removing other chars). If you then convert back tovarcharagain, you have your original datatype.