Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Last active July 24, 2024 10:00
Show Gist options
  • Save ststeiger/e1934670e4bc70ef3bca429d23c00347 to your computer and use it in GitHub Desktop.
Save ststeiger/e1934670e4bc70ef3bca429d23c00347 to your computer and use it in GitHub Desktop.
List computed columns definition
SELECT
s.name AS schema_name
,t.name AS table_name
,cc.name AS column_name
,cc.definition AS computed_column_definition
,COLUMNPROPERTY(cc.object_id, cc.name, 'ordinal') AS ordinal_position
-- ,sys_ordinal_position.derived_ordinal_position
FROM sys.computed_columns AS cc -- damn, object_id refers to table
INNER JOIN sys.tables t ON cc.object_id = t.object_id
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
/*
LEFT JOIN
(
SELECT
sys_columns.object_id AS table_object_id
,sys_columns.name AS table_column_name
,ROW_NUMBER() OVER (PARTITION BY sys_columns.object_id ORDER BY sys_columns.column_id) AS derived_ordinal_position
FROM sys.columns AS sys_columns
) AS sys_ordinal_position
ON sys_ordinal_position.table_object_id = t.object_id
AND sys_ordinal_position.table_column_name = cc.name
*/
WHERE (1=1)
AND cc.is_computed = 1
-- AND cc.definition NOT LIKE '%try_cast%'
-- ISNULL, CONVERT, ISNUMERIC, PATINDEX
-- NULLIF, LEN, CHARINDEX
-- UPPER, LEFT, REPLACE, LTRIM, RTRIM, SUBSTRING
-- datepart(year,[PTL_Datum])
-- datepart(month,[PTL_Datum])
-- datepart(day,[PTL_Datum])
-- datepart(hour,[PTL_Datum])
-- datepart(minute,[PTL_Datum])
-- datepart(second,[PTL_Datum])
-- EXTRACT(YEAR FROM your_date_column) AS year,
-- EXTRACT(MONTH FROM your_date_column) AS month,
-- EXTRACT(DAY FROM your_date_column) AS day,
-- EXTRACT(HOUR FROM your_date_column) AS hour,
-- EXTRACT(MINUTE FROM your_date_column) AS minute,
-- EXTRACT(SECOND FROM your_date_column) AS second
ORDER BY schema_name, table_name, ordinal_position
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment