Mindy, the problem is that you have the data stored in a CHAR(32)... trailing blanks come into play there. Either change the column to a VARCHAR(32) or modify my code as follows...
DECLARE @TestData TABLE(page char(32))
insert @testdata (page)
select 'G18' union all
select 'G2' union all
select 'D99' union all
select 'A08' union all
select '16' union all
select 'ENT' union all
select 'M183' union all
--===== This is the solution for the known constraints on the data as posted
DECLARE @MaxWidth TINYINT
SET @MaxWidth = (SELECT MAX(LEN(page)) FROM @TestData)
SELECT Original = page,
Modified = LEFT(d.page,d.LastLetter)
FROM (--==== Derived table "d" finds the interface between letters/digits
SELECT Page = RTRIM(page),
LastLetter = PATINDEX('%[a-z][0-9]%',RTRIM(page)+'0')
is pronounced ree-bar and is a Modenism for R
First step towards the paradigm shift of writing Set Based code: Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Helpful Links:
How to post code problemsHow to post performance problemsForum FAQs