When working with large databases in SQL Server, it is often necessary to find columns that follow a specific naming pattern. One such scenario is identifying all columns whose names end with a specific suffix. This can be useful for database maintenance, debugging, auditing, or dynamically querying related data.
This tutorial covers multiple ways to achieve this in SQL Server, catering to both beginners and advanced users.
Finding columns with a specific suffix can help in:
_date
._temp
.SQL Server provides multiple ways to search for column names that end with a specific suffix. Below are three commonly used methods.
INFORMATION_SCHEMA.COLUMNS
The INFORMATION_SCHEMA.COLUMNS
view contains metadata about all columns in the database, making it a straightforward option to find columns by suffix.
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%_date';
sys.columns
The sys.columns
system catalog view provides another method to retrieve column names based on suffix filtering.
SELECT c.name AS ColumnName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%_date';
sys.objects
and sys.columns
The sys.objects
and sys.columns
system catalog views store database objects and columns, respectively. This method is useful when more metadata is required.
SELECT c.name AS ColumnName, o.name AS TableName
FROM sys.columns c
JOIN sys.objects o ON c.object_id = o.object_id
WHERE o.type = 'U'
AND c.name LIKE '%_date';
Here, 'U
' represents user tables.
Let’s consider a database with the following tables and columns:
Table Name |
Column Name |
Employees |
hire_date |
Orders |
order_date |
Customers |
customer_name |
Products |
expiry_date |
If we execute the following query:
SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%_date';
We get:
COLUMN_NAME TABLE_NAME
------------- -----------
hire_date Employees
order_date Orders
expiry_date Products
This confirms that only the columns ending with _date are retrieved.
Organizations often store date-related data with a common suffix like _date
. You can quickly list all date-related columns using:
SELECT name
FROM sys.columns
WHERE name LIKE '%_date';
Developers sometimes create temporary columns ending with _temp
for staging data. To find such columns:
SELECT c.name AS ColumnName, t.name AS TableName
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%_temp';
If you need to drop all columns ending with _temp
, you can generate dynamic SQL:
DECLARE @sql NVARCHAR(MAX) = '';
SELECT @sql = @sql + 'ALTER TABLE ' + t.name + ' DROP COLUMN ' + c.name + '; '
FROM sys.columns c
JOIN sys.tables t ON c.object_id = t.object_id
WHERE c.name LIKE '%_temp';
EXEC sp_executesql @sql;
INFORMATION_SCHEMA.COLUMNS
, sys.columns
, and sys.objects
.LIKE
operator is essential for pattern matching in column names.Finding columns with a specific suffix in SQL Server is a crucial task for database administrators and developers. Using INFORMATION_SCHEMA.COLUMNS
, sys.columns
, and sys.objects
, we can efficiently retrieve and manage columns based on naming conventions. These techniques help in organizing and optimizing database operations while making querying more efficient.
Comments