This is how I implement the logic but it was not working
Data source/expected result
17A Post-1986 depreciation adjustment
17B Adjusted gain or loss
17C Depletion (other than oil & gas)
17D Oil, gas, & geothermal—gross income
17E Oil, gas, & geothermal—deductions
17F Other AMT items
18A Tax-exempt interest income
18B Other tax-exempt income
18C Nondeductible expenses
20C Fuel tax credit information
20D Qualified rehabilitation expenditures (other than rental real estate)
20E Basis of energy property
20F Recapture of low-income housing credit (section 42(j)(5))
20G Recapture of low-income housing credit (other)
20H Recapture of investment credit
20I Recapture of other credits
20J Look-back interest—completed long-term contracts
20K Look-back interest—income forecast method
20L Dispositions of property with section 179 deductions
20M Recapture of section 179 deduction
20N Interest expense for corporate partners
20O Section 453(l)(3) information
20P Section 453A(c) information
20Q Section 1260(b) information
20R Interest allocable to production expenditures
20S CCF nonqualified withdrawals
20T Depletion information—oil and gas
20V UBTI Ordinary
20V UBTI Capital Gain/(Loss) & Sec. 1256
20W Precontribution gain (loss)
20X Reserved
20Y Net investment income
20Z Section 199A information
20AA Section 704(c) information
20AB Section 751 gain (loss)
20AC Section 1(h)(5) gain (loss)
20AD Deemed section 1250 unrecaptured gain
20AE Excess taxable income
20AF Excess business interest income
20AG Gross receipts for section 59A(e)
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
20AH Other information
M-1 Unrealized & Other Book-Tax Differences
M-1 Tax Exempt Income
M-1 Non-Ded Expense
M-1 U.S. Tax Withholding
M-2 Other Increase/(Decrease)
drop Table if exists #temp2
SELECT dbo.udf_GetNumber([K1LineNo]) number,K1LineName,[K1LineNo],
PATINDEX('%[^0-9]%',[K1LineNo]) num_index,
STUFF([K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]), 2, '' ) num_val,
SUBSTRING( [K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]) , 50 ) string_val,
OVER() AS TotalRows ,row_number() over (
Order By CASE
WHEN ISNUMERIC([K1LineNo])=1
THEN CAST([K1LineNo] as int)
WHEN PATINDEX('%[^0-9]%',[K1LineNo]) > 1
THEN CAST( LEFT( [K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]) - 1) as int)
ELSE 2147483648
END,
CASE
WHEN ISNUMERIC([K1LineNo])=1
THEN NULL
WHEN PATINDEX('%[^0-9]%',[K1LineNo]) > 1
THEN SUBSTRING( [K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]) , 50 )
ELSE [K1LineNo]
END ,
CASE
WHEN LEN(SUBSTRING( [K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]) , 50 ) )=2
THEN STUFF([K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]), 2, '' )
ELSE [K1LineNo]
END
)*100 as r_n
into #temp2
FROM [Table] WHERE [K1LineNo]!=''
ORDER BY
CASE
WHEN ISNUMERIC(K1LineNo)=1
THEN CAST(K1LineNo as int)
WHEN PATINDEX('%[^0-9]%',K1LineNo) > 1
THEN CAST( LEFT( K1LineNo, PATINDEX('%[^0-9]%',K1LineNo) - 1) as int)
ELSE 2147483648
END,
CASE
WHEN ISNUMERIC(K1LineNo)=1
THEN NULL
WHEN PATINDEX('%[^0-9]%',K1LineNo) > 1
THEN SUBSTRING( K1LineNo, PATINDEX('%[^0-9]%',K1LineNo) , 50 )
ELSE K1LineNo
END ,
CASE
WHEN LEN(SUBSTRING( [K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]) , 50 ) )=2
THEN STUFF([K1LineNo], PATINDEX('%[^0-9]%',[K1LineNo]), 2, '' )
ELSE [K1LineNo]
END
SELECT * FROM #temp2