Last active
April 6, 2022 14:57
-
-
Save stevewithington/da05bc97931d6ed9c99a8dc06cfd283f to your computer and use it in GitHub Desktop.
SQL: CASE WHEN LIKE RegEx example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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