September 2019 | VirtualSpecies

SQL Column Names From Table

Below is the effective query to get column names from a SQL table. We will use INFORMATION_SCHEMA to get the task done.

1
2
3
SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = N'TableName'
In the alternate example below, we will use sys.columns to get the task done.

SQL Retrieve Email Domains

If we need to retrieve all the email domains from a SQL table, we can easily pull up result using below script. The SUBSTRING function is used to extract and display the part of a string. In this case, we have retrieved email domains after the @ symbol in each record.

1
2
3
SELECT DISTINCT 
SUBSTRING(Email, CHARINDEX('@',Email) + 1, LEN(Email) - CHARINDEX('@',Email)) 
FROM dbo.EmailRecord
Even better with script below, we will be able to count how many of each domains are existed in the given table.