Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
SQL

Get the User Name and Domain Name from an Email Address in SQL

- 11.06.18 | 24.03.26 - ErcanOPAK

The Challenge: You have a Users table with thousands of email addresses, and you need to generate a report that separates the Username from the Domain Name. Doing this manually is impossible, and using complex RegEx in SQL can be overkill.

The Solution: By combining three core SQL functions—SUBSTRING, CHARINDEX, and LEN—you can perform surgical strikes on strings to extract exactly what you need.


🚀 The “Clean Code” Script

This script dynamically calculates the position of the @ symbol and slices the string into two distinct parts. Copy and run this in your SSMS to see the magic:

DECLARE @Email VARCHAR(100) = 'info@ercanopak.com';

SELECT 
    -- 1. Extract everything BEFORE the '@'
    SUBSTRING(@Email, 1, CHARINDEX('@', @Email) - 1) AS [User Name],
    
    -- 2. Extract everything AFTER the '@'
    SUBSTRING(@Email, CHARINDEX('@', @Email) + 1, LEN(@Email)) AS [Domain Name];
Output:

User Name Domain Name
info ercanopak.com

🔍 How the Logic Works (The “Pro” Breakdown)

To truly master SQL, you need to understand why this works. Here is the mechanical breakdown of the functions used:

  • CHARINDEX('@', @Email): This is our “GPS”. It finds the exact numerical position of the @ character.
  • The -1 and +1 Logic:
    • We use -1 for the Username because we want to stop just before the @.
    • We use +1 for the Domain because we want to start just after the @.
  • LEN(@Email): This tells the SUBSTRING function to keep reading until the very end of the string, ensuring no domain (no matter how long) is cut off.

⚠️ Pro-Tip: Handling “Null” or Invalid Emails

If your column contains rows without an @ symbol, the query above might throw an error. Use a CASE statement or NULLIF to make it bulletproof:

SELECT 
    CASE WHEN CHARINDEX('@', Email) > 0 
         THEN SUBSTRING(Email, 1, CHARINDEX('@', Email) - 1) 
         ELSE 'Invalid Email' END AS CleanUsername
FROM Users;

Summary

String manipulation is a vital skill for data cleaning and ETL processes. Mastering these three functions allows you to handle not just emails, but URLs, file paths, and any delimited data directly in SQL.

Related posts:

SQL Deadlocks Appear Sporadically

How to use SQL RAISEERROR() messages in C#

How to solve the stucking on "Loading Packages" phase for SSMS installation

Post Views: 950

Post navigation

Get the First and Last Word from a String or Sentence in SQL
Convert a Comma-Delimited List to a Table in SQL

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (448)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (859)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (754)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com