• The following doesn't use an UDF or CLR, and seems to cover all the (4) scenarios you described above.

    declare @Invoice as table( BillID varchar(20) primary key );

    insert into @Invoice ( BillID )

    values ('A6304158-2'),('A6304158'),('6304158-2'),('6304158');

    select BillID

    , substring

    (

    BillID

    , patindex('%[0-9]%',BillID)

    , len(BillID) - patindex('%[0-9]%',BillID) + 1

    - case when patindex('%-%',BillID) > 0

    then len(BillID) - patindex('%-%',BillID) + 1

    else 0

    end

    ) as NewBillID

    from @Invoice;

    BillID NewBillID

    6304158 6304158

    6304158-2 6304158

    A6304158 6304158

    A6304158-2 6304158

    "Do not seek to follow in the footsteps of the wise. Instead, seek what they sought." - Matsuo Basho