SQL Server Select Command - Extract first 2 letters from a column and prefix

I need a SQL Select Command that will:

1. Extract the 1st 2 chars from a column named 'Product_Desc' and then remove spaces.   For example;  1d =  '1 dThis is a test'
2. Prefix the extracted results with 'QB_Index_'.  For example; QB_Index_1d
3. If no value in the 'Product_Desc', then default value to 'QB_OB'

Select Product_Desc from [mytable].  

Sample Data and Expected Result
Product_Desc                                                                NewColumn
1 DIA CARBIDE ENDMILL 4F SE CC                             QB_Index_1D
AMY 50W LO VOLT TRANS F/ CAB LGHT                    QB_Index_AM
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
You want to use the following SQL Functions:


               'QB_Index_' +  SUBSTRING(REPLACE(Product_Desc, ' ', ''), 1, 2)  AS [NewColumn]
FROM MyTable
Daniel Van Der WerkenIndependent ConsultantCommented:
Oops, I forgot the empty row. You also want to use a CASE

               CASE WHEN LEN(LTRIM(RTRIM(Product_Desc))) > 0 THEN 'QB_Index_' +  SUBSTRING(REPLACE(Product_Desc, ' ', ''), 1, 2)  ELSE 'QB_Index_OB' END AS [NewColumn]
FROM MyTable

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Vitor MontalvãoMSSQL Senior EngineerCommented:
Try this:
SELECT Product_Desc, 'QB_Index_' + LEFT(REPLACE(ISNULL(Product_Desc,'OB'),' ',''),2) AS QB_Index
FROM ProductDesc

Open in new window

tmajor99Author Commented:
Very Nice!!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.