Question

I have two tables. One table has product information with a column ProdNum and the second table has all the related product documents. Those prodcut document name format is like this: ProdNumxxxx.pd So all documents' five first digits are related to a ProdNum then it has letters and other number. I need to join these two tables to find the documents related to the product number. Now I can't join it on ProdNum and ProdFile because obviously they do not match. I was thinking of creating another column that will pick up the first five characters of ProdFile name and then create a column on those first five characters that way I can do JOIN on it to match the ProdNum. I have absolutely no clue how to do this. Any ideas/opinions?

Était-ce utile?

La solution

Here is a query that you can use to handle filenames that have less than 5 numbers:

select 
    ProdFileInfo.* 
FROM ProdFileInfo 
INNER JOIN ProdInfo 
ON ProdInfo.ProdNum = (CASE 
                           WHEN LEN(ProdFileInfo.ProdFile) < 5 THEN CAST(SUBSTRING(ProdFileInfo.ProdFile, 1, LEAST(5) AS decimal)
                           ELSE CAST(SUBSTRING(ProdFileInfo.ProdFile, 1, 5) AS decimal)
                       END);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top