Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save stevewithington/600cc252cd141432211025e320e4724f to your computer and use it in GitHub Desktop.
Save stevewithington/600cc252cd141432211025e320e4724f to your computer and use it in GitHub Desktop.
SQL: Update Field To NULL If String Found In Filter Strings Temp Table
DECLARE @filter_strings TABLE (
filter_string NVARCHAR(255) NOT NULL
);
INSERT INTO @filter_strings(filter_string)
VALUES ('test'), ('city'), ('state'), ('title'), ('street'), ('1'), ('-'), ('.'), ('na'), ('n/a'), ('x'), ('?'), ('unknown'), ('asdf'), ('abcd'), ('abc'), ('abd')
;
UPDATE [dbo].[someTable]
SET
[postal_code] =
CASE
WHEN LEN(COALESCE([postal_code], [billing_postal_code], [inferred_postal_code])) = 4 AND COALESCE([country], [billing_country], [inferred_country]) IN ('US', 'United States')
THEN UPPER(CONCAT('0', COALESCE([postal_code], [billing_postal_code], [inferred_postal_code])))
ELSE UPPER(COALESCE([postal_code], [billing_postal_code], [inferred_postal_code]))
END
, [city] =
CASE
WHEN COALESCE([city], [billing_city], [inferred_city]) IN (SELECT filter_string FROM @filter_strings)
THEN NULL
ELSE LEFT(COALESCE([city], [billing_city], [inferred_city]), 80)
END
, [title] =
CASE
WHEN [title] IN (SELECT filter_string FROM @filter_strings)
THEN NULL
ELSE LEFT([title], 100)
END
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment