
DELETE FROM pricing.target_prices;

INSERT INTO 
	pricing.target_prices
SELECT
	stlc,
	ds,
	chan,
	tier,
	vol,
	-- Extract lower bound: text between '[' and ','
	TRY_CAST(SUBSTRING(vol, 2, CHARINDEX(',', vol) - 2) AS INT) AS lower_bound,
	-- Extract upper bound: text between ',' and ')'
	CASE
		WHEN RIGHT(vol, 2) = ',)' THEN NULL
		ELSE TRY_CAST(SUBSTRING(vol, CHARINDEX(',', vol) + 1, LEN(vol) - CHARINDEX(',', vol) - 1) AS INT)
	END AS upper_bound,
	price,
	math
FROM
	usmidsap02.ubm.pricequote.target_prices_view;
