;WITH test_data AS (
SELECT CAST(N'ADMN01' AS nvarchar(12)) AS value UNION ALL
SELECT 'ARA' UNION ALL
SELECT 'ARA000'
)
SELECT
td.value,
LEFT(td.value, first_digit - 1) + RIGHT(REPLICATE('0', LEN(digits) - 1) +
CAST(calc2.digits + 1 AS varchar(30)), LEN(digits)) AS new_value
FROM test_data td
CROSS APPLY (
SELECT CASE WHEN PATINDEX('%[0-9]%', value) = 0 THEN LEN(value) + 1
ELSE PATINDEX('%[0-9]%', value) END AS first_digit
) AS calc1
CROSS APPLY (
SELECT CASE WHEN PATINDEX('%[0-9]%', value) = 0 THEN '0000'
ELSE SUBSTRING(value, first_digit, 10) END AS digits
) AS calc2
i am trying to do this
why you may ask .. for my own noodles .. for my own practice , for my own stats .. cognition etc etc etc
i put some edge cases in the data
create data script
drop table if exists #Temp
create table #Temp ( Col1 varchar(20))
insert into #Temp select 'ADMN01'
insert into #Temp select 'ADMN1234'
insert into #Temp select 'ADMN1239'
insert into #Temp select 'ARA'
insert into #Temp select 'ARA000'
Scotts SQL
SELECT
td.Col1,
LEFT(td.Col1, first_digit - 1) + RIGHT(REPLICATE('0', LEN(digits) - 1) +
CAST(calc2.digits + 1 AS varchar(30)), LEN(digits)) AS new_Col1
FROM #Temp td
CROSS APPLY (
SELECT CASE WHEN PATINDEX('%[0-9]%', Col1) = 0 THEN LEN(Col1) + 1
ELSE PATINDEX('%[0-9]%', Col1) END AS first_digit
) AS calc1
CROSS APPLY (
SELECT CASE WHEN PATINDEX('%[0-9]%', Col1) = 0 THEN '0000'
ELSE SUBSTRING(Col1, first_digit, 10) END AS digits
) AS calc2