How to Get a Computed Column’s Definition in SQL Server using T-SQL

In SQL Server, you can use T-SQL to get the definition for a computed column by querying the sys.computed_columns system catalog view.

Example 1 – Return One Computed Column

Here’s an example that I ran in my test environment. In this case I narrowed the results to just one computed column.

SELECT definition
FROM sys.computed_columns
WHERE name = 'TotalValue';

Result:

+----------------------+
| definition           |
|----------------------|
| ([Quantity]*[Price]) |
+----------------------+

In case you have multiple computed columns with the same name, you could also add the table name to the WHERE clause:

SELECT definition
FROM sys.computed_columns
WHERE name = 'TotalValue'
AND OBJECT_NAME(object_id) = 'Products';

Example 2 – Return All Computed Columns

In this example I return all computed columns, along with their definition.

SELECT 
  OBJECT_NAME(object_id) AS [Table],
  name AS [Computed Column],
  definition
FROM sys.computed_columns;

Result:

+----------+-------------------+--------------------------------------+
| Table    | Computed Column   | definition                           |
|----------+-------------------+--------------------------------------|
| Person   | FullName          | (concat([FirstName],' ',[LastName])) |
| Products | TotalValue        | ([Quantity]*[Price])                 |
+----------+-------------------+--------------------------------------+

Example 3 – Include the Schema

In this example I join with sys.objects to include the schema in the results.

SELECT 
  SCHEMA_NAME(o.schema_id) AS [Schema],
  OBJECT_NAME(cc.object_id) AS [Table],
  cc.name AS [Computed Column],
  cc.definition
FROM sys.computed_columns cc
INNER JOIN sys.objects o
ON o.object_id = cc.object_id;

Result:

+----------+----------+-------------------+--------------------------------------+
| Schema   | Table    | Computed Column   | definition                           |
|----------+----------+-------------------+--------------------------------------|
| dbo      | Person   | FullName          | (concat([FirstName],' ',[LastName])) |
| dbo      | Products | TotalValue        | ([Quantity]*[Price])                 |
+----------+----------+-------------------+--------------------------------------+