SQL Syntax, Function, Insert data

Hi Experts,
I select the following 3 columns from two tables:
Table1.AliasName
Table1.HostName
Table2.ProductType

I'm inserting these 3 columns to Table3, where I added a new column, namely ProductId.
So, Table3 has 4 columns: AliasName, HostName, ProductType, ProductId

The content of the ProductId column has to be generated based on what my query returns from the AliasName, HostName, ProductType columns.
For this I would need a function.

This it the pseudo code:
Is ProductType "Vegetable" OR "Dairy" AND HostName start with "PACKAGE"
  If/Validate/Parse that AliasName is 6 HEX digits (0-F)
  Return "AAA.BBB.CCC.AAAAAA.1"+AliasName
 
Is ProductType "Fruit", "Meat", "Grocery" or "Plants"
 If HostName starts with "USA"
 If/Validate/Parse AliasName is 6 NUMERIC (0-9) digits
Return "AAA.BBB.CCC.BBBBBB.1"+AliasName

Is HostName start with "PACKAGE"
If/Validate/Parse that AliasName is 6 HEX digits (0-F)
Return "AAA.BBB.CCC.CCCCCC.1"+AliasName

If/Validate/Parse that AliasName is 5 or 7 UPPERCASE ALPHA (A-Z)
Return "AAA.BBB.CCC.DDDDDD.1"+AliasName

What is the correct syntax of the query to achieve this. I would appreciate your help.
romsomIT DeveloperAsked:
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.

Scott PletcherSenior DBACommented:
To do the UPPERCASE check, I have to refresh my memory on COLLATE bin values:

CASE
    WHEN ProductType IN ('Dairy', 'Vegetable') AND
         HostName LIKE 'PACKAGE%' AND
         AliasName LIKE '[0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF]'
    THEN 'AAA.BBB.CCC.AAAAAA.1'
    WHEN ProductType IN ('Fruit', 'Grocery', 'Meat', 'Plants') AND
         HostName LIKE 'USA%' AND
         AliasName LIKE '[0-9][0-9][0-9][0-9][0-9][0-9]'
    THEN 'AAA.BBB.CCC.BBBBBB.1'
    WHEN HostName LIKE 'PACKAGE%' AND
         AliasName LIKE '[0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF][0-9ABCDEF]'
    THEN 'AAA.BBB.CCC.CCCCCC.1'
    WHEN AliasName COLLATE Latin1_General_CS_AS LIKE '[A-Z][A-Z][A-Z][A-Z][A-Z]' OR
         AliasName COLLATE Latin1_General_CS_AS LIKE '[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]' OR
         AliasName COLLATE Latin1_General_CS_AS LIKE '[A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]'
    THEN 'AAA.BBB.CCC.DDDDDD.1'
END AS AliasName
0
romsomIT DeveloperAuthor Commented:
Thank you for your quick response. I'm still trying to make it work.
My 4th column name has to be called "ProductId" and the record stored in it has to have the following format:
AAA.BBB.CCC.AAAAAA.1.XXXX
where XXXX is the AliasName

Is the syntax correct if after THEN I write 'AAA.BBB.CCC.AAAAAA.1.+AliasName' ?
Or is it correct if I write
THEN 'AAA.BBB.CCC.AAAAAA.1.'+AliasName ?

At the end I should write: END AS ProductId, right?
0
Scott PletcherSenior DBACommented:
Yes, you've got it exactly right.  Concatenate AliasName and specify the desired column name after the "AS".

WHEN ...
THEN 'AAA.BBB.CCC.AAAAAA.1.' + AliasName
...
END AS ProductID
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott PletcherSenior DBACommented:
So why isn't my answer selected and awarded points since it worked?!
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
romsomIT DeveloperAuthor Commented:
Sorry, I viewed it from my cell phone and accidentally I clicked the wrong button. Are the points awarded now? I apologize, my mistake.
0
Scott PletcherSenior DBACommented:
Yes, sorry, didn't realize you used your phone, that really is rather tricky.  It's all good now.
1
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
SQL

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.