Help with MS SQL Server Select Command

I need help creating SQL Select command to perform a look up against another table with the ability to transform the values.  Not sure if this is even possible with a SQL Server but thought I would give it try anyway.  So here is what I need.  

I need a SQL Select command to match data from table1 to table2 based on this rule:
  -  remove spaces from number
  -  If length of Table1.Number = 14,
         search Table2.GTIN Number Column
  -  ElseIF length is 10,
         Search on UPC Column
 
Table1    (1 column)                      Table2  ( 2 columns)
-----------------------------                     --------------------------------------------------
Number                                           UPC Number                        GTIN Number
000 38000 10061 1                         0000000000                        00038000100611
38000 00010                                    3800000010

Expected Results:
COL1
00038000100611
3800000010
hojohappyAsked:
Who is Participating?
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.

Brian CroweDatabase AdministratorCommented:
try this...

WITH cteTable1 (Number)
AS
(
   SELECT REPLACE(Table1.Number, ' ', '') AS Number
   FROM Table1
)
SELECT ISNULL(T2UPC.[UPC Number], T2GTIN.[GTIN Number])
FROM cteTable1 AS T1
LEFT OUTER JOIN Table2 AS T2UPC
   ON LEN(T1.Number) = 10
   AND T1.Number = T2UPC.[UPC Number]
LEFT OUTER JOIN Table2 AS T2GTIN
   ON LEN(T1.Number) = 14
   AND T1.Number = T2GTIN.[GTIN Number]
WHERE T2UPC.[UPC Number] IS NOT NULL
   OR T2GTIN.[GTIN.Number] IS NOT NULL
0

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
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
Regular Expressions

From novice to tech pro — start learning today.