Redshift-specific string and conditional functions are converted as follows:
REGEXP_COUNT(str, pattern) is converted to a combination of LENGTH and REGEXP_REPLACE: LENGTH(str) - LENGTH(REGEXP_REPLACE(str, pattern, '')).DECODE(expr, v1, r1, v2, r2, default) is converted to an inline CASE WHEN expr=v1 THEN r1 WHEN expr=v2 THEN r2 ELSE default END statement.CHARINDEX(substr, str) is converted to INSTR(str, substr) (note that the argument order is swapped).
Example conversion:
-- Redshift
SELECT REGEXP_COUNT(email, '[A-Z]'), DECODE(val, 1, 'A', 2, 'B', 'C');
-- Databricks
SELECT LENGTH(email) - LENGTH(REGEXP_REPLACE(email, '[A-Z]', '')), CASE WHEN val=1 THEN 'A' WHEN val=2 THEN 'B' ELSE 'C' END;
-- Redshift
UPDATE customers
SET loyalty_points = loyalty_points +
CASE
WHEN REGEXP_COUNT(email, '[A-Z]') > 3 THEN 100
WHEN REGEXP_COUNT(email, '[0-9]') > 2 THEN 50
ELSE 25
END,
status = DECODE(
REGEXP_COUNT(phone, '[0-9]'),
10, 'VERIFIED',
11, 'VERIFIED',
'PENDING'
)
WHERE customer_id IN (
SELECT DISTINCT o.customer_id
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days'
AND o.total_amount > 500
);
-- Databricks
UPDATE customers
SET loyalty_points = loyalty_points +
CASE
WHEN LENGTH(email) - LENGTH(REGEXP_REPLACE(email, '[A-Z]', '')) > 3 THEN 100
WHEN LENGTH(email) - LENGTH(REGEXP_REPLACE(email, '[0-9]', '')) > 2 THEN 50
ELSE 25
END,
status = case when LENGTH(phone) - LENGTH(REGEXP_REPLACE(phone, '[0-9]', ''))=10 then 'VERIFIED' when LENGTH(phone) - LENGTH(REGEXP_REPLACE(phone, '[0-9]', ''))=11 then 'VERIFIED' else 'PENDING' END
WHERE customer_id IN (
SELECT DISTINCT o.customer_id
FROM orders o
JOIN order_items oi ON o.order_id = oi.order_id
WHERE o.order_date >= CURRENT_DATE - INTERVAL '30 days'
AND o.total_amount > 500
);