SQL Server REGEXP_REPLACE for Email Normalization

DP-800 By islever Editorial Last updated

Short answer

C Use REGEXP_REPLACE to substitute the substring from the plus sign up to the @ symbol with just the @ symbol.

Question

You have a SQL database in Microsoft Fabric that contains a column named Payload. Payload stores customer data in JSON documents that have the following format.
Exhibit: SQL Server REGEXP_REPLACE for Email Normalization
Data analysis shows that some customers have subaddressing in their email address, for example, [email protected] .
You need to return a normalized email value that removes the subaddressing, for example, user1 [email protected] must be normalized to [email protected] .
Which Transact-SQL expression should you use?
  1. REGEXP_REPLACE(JSON_VALUE(Payload, ‘$.customer_email’), ‘\+.*$’, ‘’)
  2. REGEXP_SUBSTR(JSON_VALUE(Payload, ‘$.customer_email’), ‘^[^+]+@.*$=’)
  3. REGEXP_REPLACE(JSON_VALUE(Payload, ‘$.customer_email’), ‘\+.*@’, ‘@’) Correct Answer
  4. REGEXP_REPLACE(JSON_VALUE(Payload, ‘$.customer_email’), ‘\+.*’, ‘’)

Key Takeaways

The key is understanding how regex replacement works: you must match the unwanted portion (from the plus sign up to the @ symbol) and replace it with just the @ symbol, preserving the domain.

This question tests the ability to use regular expressions in T-SQL to normalize email addresses by removing subaddressing (the plus sign and everything following it). The correct expression uses a regex pattern that matches the plus sign up to the domain separator.

Common mistake: Learners often choose option A, which replaces everything from the plus sign to the end of the string. This incorrectly removes the domain name (e.g., 'example.com'), leaving only the local part without a valid email format.

Expert Analysis

Why the Answer Is Correct

Option C is the correct choice because the regex pattern '\+.*@' specifically targets the plus sign, any characters following it (non-greedy or greedy until the next specific delimiter), and stops at the '@' symbol. By replacing this matched sequence with just '@', the subaddressing is removed while retaining the full domain. For example, '[email protected]' becomes '[email protected]'.

Why the Other Options Are Wrong

Option A replaces '\+.$' (plus sign to end of string) with an empty string, resulting in 'user1' without the domain. Option B uses REGEXP_SUBSTR (which extracts rather than replaces) and has a syntax error with '=)' at the end. Option D replaces '\+.' (plus sign to end of string) with an empty string, also dropping the domain.

Community Comment Notes

One commenter noted that '$' means until the end of the string, correctly identifying why Option A fails to preserve the domain. Another user questioned if removing the plus and everything after leaves just the username, highlighting the common confusion between stripping the suffix versus stripping the entire rest of the string.

Exam Strategy

When using regex replacement, always verify what remains after the substitution. If you are removing text, ensure you aren't accidentally deleting necessary parts like the domain in an email address. Use '@' as a boundary marker when dealing with emails.

Frequently Asked Questions

Why does Option A fail to normalize the email?

Option A replaces everything from the '+' to the end of the string with nothing, deleting the domain name entirely.

What does the regex \+.*@ actually match?

It matches the literal plus sign, followed by any characters, stopping right before the '@' symbol.

More DP-800 FAQ →

Discussion 4 contributions

Compiled from public community threads and reader submissions, lightly edited for clarity. Handles and vote tallies are shown as recorded, and comments reflect the views of individual contributors.

From public community threads 4

  1. dabhelia 👍 1 Answered C
    Why C is correct: JSON_VALUE(Payload, '$.customer_email') pulls the email string out of the JSON document. + matches a literal plus sign, .* matches the subaddress tag, and @ anchors the match at the start of the domain. Replacing that match with @ removes only the tag and keeps the local part and the domain. An address without a + doesn't match the pattern, so it's returned unchanged.
  2. Jaz_Muthini 👍 1
    wait... but does that not mean remove the plus sign and everything after it, leaving us with just user1 ?
  3. Sr18 👍 1 Answered C
    C: Pattern provided will remove anything from + to last @ and replacing it with @ will do the work.
  4. BeingAbhi 👍 1 Answered A
    $ means until the end of the string. So, A is correct.

Reader comments & corrections

No reader corrections yet — spotted an error or have a note? Share it below.

Log in to comment, report an error, or add a note about this question.

← Back to DP-800 Study Guide