Ok basically what this statement is doing is going through all product names in the product table and finding the years and inserting them into a product specification attribute table. The tricky part is the products are all listed like this:
Exhaust system for 92-95 Honda Civic.
The statement currently works great for that and finds the range of 92, 93, 94 and 95 and then matches it to the specification attributes (which are 4 digit years) and inserts each one as 1992, 1993, 1994, 1995.
The problem I am running into is any product with a year range that begins or ends with 00 does not work. So Exhaust system for 00-06 Honda Civic or 98-00 Honda Civic does not work. It wont even insert the other years either.
However for a year range that has 00 in between it works. So 98-02 Honda Civic will insert all the correct years 1998, 1999, 2000, 2001, 2002.
This is causing a huge issue with my website and I appreciate any help that anyone can provide. Thank you!
Exhaust system for 92-95 Honda Civic.
The statement currently works great for that and finds the range of 92, 93, 94 and 95 and then matches it to the specification attributes (which are 4 digit years) and inserts each one as 1992, 1993, 1994, 1995.
The problem I am running into is any product with a year range that begins or ends with 00 does not work. So Exhaust system for 00-06 Honda Civic or 98-00 Honda Civic does not work. It wont even insert the other years either.
However for a year range that has 00 in between it works. So 98-02 Honda Civic will insert all the correct years 1998, 1999, 2000, 2001, 2002.
This is causing a huge issue with my website and I appreciate any help that anyone can provide. Thank you!
Code:
INSERT INTO Product_SpecificationAttribute_Mapping
(ProductId, SpecificationAttributeOptionId, AllowFiltering, ShowOnProductPage, DisplayOrder)
SELECT PD.Id AS ProductId,
sao.Id AS SpecificationAttributeOptionId,
'1' AS AllowFiltering,
'0' AS ShowOnProductPage,
sao.DisplayOrder AS DisplayOrder
FROM Product as PD
join (
-- Get product with evaluated previous century / current century ranges.
select id,
name,
ymin as pcmin,
case when ymax < ymin then 99 else ymax end as pcmax,
case when ymax < ymin then 0 else ymin end as ccmin,
ymax as ccmax
from (
-- Product name with year entries as min / max.
select id,
name,
convert(int, SUBSTRING(name, loc-2, 2)) as ymin,
convert(int, SUBSTRING(name, loc+1, 2)) as ymax
from (
-- Pick those entries where '-' is in the form _XX-YY_
select id,
name,
loc
from (
-- Find the location of the all important '-'.
select id,
name,
CHARINDEX('-', name) as loc
from PRODUCT
where CHARINDEX('-', name) > 0
) as tbl
where SUBSTRING(name, loc-3, 1) = ' '
and SUBSTRING(name, loc+3, 1) = ' '
) as rng
where convert(int, SUBSTRING(name, loc-2,2)) > 0
and convert(int, SUBSTRING(name, loc+1,2)) > 0
) as yrs
) as PdRng
on PdRng.id = PD.id
-- Join the specification where the spec is between the computed product ranges.
join (
-- Only want the entries that are 4 digit years.
select *
from SpecificationAttributeOption
where isnumeric(Name) > 0
and len(rtrim(Name)) = 4 AND SpecificationAttributeOption.SpecificationAttributeId = '7'
) as sao
ON convert(int, Right(sao.Name, 2)) between PdRng.pcmin and PdRng.pcmax
OR convert(int, Right(sao.Name, 2)) between PdRng.ccmin and PdRng.ccmax