Hey everyone. Not quite sure what happened, but the scripts in the article need a modification so they use a token for the column name: <column>.
INSERT INTO #ExpressionToDataTypeAndNull
( DATA_TYPE ,
IS_NULLABLE ,
CustomExpression
)
VALUES
( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>",')
,( 'varchar','NO','LTRIM(RTRIM("<column>")) AS "<column>",')
,( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>",')
,( 'nvarchar','NO','LTRIM(RTRIM("<column>")) AS "<column>",')
,( 'datetime','YES','CAST("<column>" AS varchar(max)) AS "<column>",')
,( 'datetime','NO','CAST("<column>" AS varchar(max)) AS "<column>",')
;
and
DECLARE @TableName nvarchar(128)
SET @TableName = 'FictionalEmployees'
SELECT
ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME + ' AS ' + COLUMN_NAME) AS ThisColumnExpression
FROM INFORMATION_SCHEMA.COLUMNS a
FULL OUTER JOIN #ExpressionToDataTypeAndNull b
ON a.DATA_TYPE = b.DATA_TYPE
AND a.IS_NULLABLE = b.IS_NULLABLE
WHERE TABLE_NAME = @TableName
ORDER BY ordinal_position ASC;
Once you have the token <column> replaced by the column name, your output includes the column names as advertised in the results.
Sorry about that! I'm not sure what happened there.
Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow