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 “Slow Pagination” — The Secret Trick: Seek Method

Replace duplicate spaces with a single space in SQL

Avoid SELECT * in Production

Post Views: 960

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 *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

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

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

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