Remove first character from Index

1

Good afternoon.

I have a varchar with some numbers

set @ID_CUSTOMERNUMBER = '01234567'

I would like to remove the zero only if it is the first number of my variable, eg

'0123456789' removing the zero would be '123456789'

'123401234' I can not remove zero, it is not the first one.

I have the command

PATINDEX('%0%', @ID_CUSTOMERNUMBER);  

but it always returns the index, regardless of whether it is first or not.

    
asked by anonymous 27.04.2017 / 18:32

1 answer

2

Just change the PATINDEX to this: %[^0]%'

And to remove the first character, you can use SUBSTRING () , in this way :

DECLARE @ID_CUSTOMERNUMBER varchar(60)
SET @ID_CUSTOMERNUMBER = '01234567';

SELECT SUBSTRING(@ID_CUSTOMERNUMBER, patindex('%[^0]%',@ID_CUSTOMERNUMBER), 10)
    
27.04.2017 / 18:41