Skip to content

Instantly share code, notes, and snippets.

@stevewithington
Last active April 6, 2022 14:57
Show Gist options
  • Save stevewithington/da05bc97931d6ed9c99a8dc06cfd283f to your computer and use it in GitHub Desktop.
Save stevewithington/da05bc97931d6ed9c99a8dc06cfd283f to your computer and use it in GitHub Desktop.
SQL: CASE WHEN LIKE RegEx example
-- Update with RegEx
UPDATE t
SET
[someField] =
CASE
WHEN LEFT(t.[postal_code], 3) LIKE '%[a-z][0-9][a-z]%'
THEN 'Canadian Value'
WHEN LEFT(t.[postal_code], 3) LIKE '%[0-9][0-9][0-9]%'
THEN 'US Value'
ELSE NULL
END
FROM [dbo].[someTable] t
WHERE 1=1
AND t.[country] IN ('US', 'Canada')
;
-- Delete with RegEx
DELETE FROM [dbo].[someTable]
WHERE 1=1
AND (
[email] NOT LIKE '_%@_%.__%'
OR [someField] IS NULL
OR [someField] NOT LIKE '%SomeString%'
)
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment