SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Query-generated Column Expressions


Query-generated Column Expressions

Author
Message
Bill Nicolich
Bill Nicolich
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 543
Comments posted to this topic are about the item Query-generated Column Expressions

Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow
Bill Nicolich
Bill Nicolich
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 543
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
ddriver
ddriver
Valued Member
Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)Valued Member (61 reputation)

Group: General Forum Members
Points: 61 Visits: 250
Try to specify the alises all in one place. Save yourself some typing. Also build the full select starement. Also could be used as a create view statement. Interesting idea though.


INSERT INTO #ExpressionToDataTypeAndNull
( DATA_TYPE ,
IS_NULLABLE ,
CustomExpression
)
VALUES
( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",'''')))')
,( 'varchar','NO','LTRIM(RTRIM("<column>"))"')
,( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",'''')))')
,( 'nvarchar','NO','LTRIM(RTRIM("<column>"))')
,( 'datetime','YES','CAST("<column>" AS varchar(max))')
,( 'datetime','NO','CAST("<column>" AS varchar(max))')
;

DECLARE @TableName nvarchar(128)
SET @TableName = 'FictionalEmployees'

DECLARE @SqlSelect NVARCHAR(MAX)

SELECT @SqlSelect = COALESCE(
@SqlSelect + ', ' + ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
, ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
) + ' AS "' + COLUMN_NAME + '"'
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;


SET @SqlSelect = 'SELECT ' + @SqlSelect + 'FROM ' + @TableName
PRINT @SqlSelect
-- EXECUTE sp_executesql @SqlSelect


Nick Chadwick
Nick Chadwick
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 16
Hi.

An interesting article! I just thought I'd point out that:

"FULL OUTER JOIN allows the query to return columns that don't have a match in the #ExpressionToDataTypeAndNull table"

is not 100% right. What you are describing is a LEFT outer join.

A FULL outer join would return results in the opposite case too - i.e. if there were row(s) in the #ExpressionToDataTypeAndNull table for which there is no match in INFORMATION_SCHEMA.COLUMNS.

However, as you are specifying in your WHERE clause that TABLE_NAME = @TableName, you are effectively converting your full outer join into a left outer join, (perhaps unknowingly!)

Just wanted to clarify that, in case other readers find it misleading!

Keep up the good work.

Cheers,
Nick
Bill Nicolich
Bill Nicolich
SSC-Enthusiastic
SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)SSC-Enthusiastic (193 reputation)

Group: General Forum Members
Points: 193 Visits: 543
Nick Chadwick (5/25/2010)
Hi.

An interesting article! I just thought I'd point out that:



Thanks Nick! You put it nicely. It's very much the case here at SSC.COM that ones peers have enough passion and interest to find something if it's amiss or point out nuances. That's one reason why I like to contribute - for the challenge and learning experience of it.

If you don't believe me, put something in and give it a shot!

I didn't want to lengthen the article too much, but one point I'll add is that with this technique, one can build a little library of functions and keep it handy for those ETL, Ad-hoc or other situations.

Bill Nicolich: www.SQLFave.com.
Daily tweet of what's new and interesting: AppendNow
Douglas Osborne-456728
Douglas Osborne-456728
SSC Veteran
SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)SSC Veteran (208 reputation)

Group: General Forum Members
Points: 208 Visits: 327
Nick,

This seemed to work a little better for me:


CREATE TABLE #ExpressionToDataTypeAndNull
(
DATA_TYPE VARCHAR(15),
IS_NULLABLE VARCHAR(3) ,
CustomExpression VARCHAR(128)
)

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'varchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'varchar','NO','LTRIM(RTRIM("<column>")) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'nvarchar','YES','LTRIM(RTRIM(ISNULL("<column>",''''))) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'nvarchar','NO','LTRIM(RTRIM("<column>")) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'datetime','YES','CAST("<column>" AS varchar(max)) AS "<column>"')

INSERT INTO #ExpressionToDataTypeAndNull( DATA_TYPE , IS_NULLABLE , CustomExpression )
VALUES ( 'datetime','NO','CAST("<column>" AS varchar(max)) AS "<column>"')

DECLARE @TableName VARCHAR(128)
SET @TableName = 'EntityRelationshipCache'

DECLARE @SqlSelect NVARCHAR(MAX)

SELECT @SqlSelect = COALESCE( @SqlSelect + ', ' + ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME )
, ISNULL(REPLACE(CustomExpression,'<column>', COLUMN_NAME), COLUMN_NAME ) )
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;

SET @SqlSelect = 'SELECT ' + REPLACE( @SqlSelect, '"', '') + ' FROM ' + @TableName

PRINT @SqlSelect

--EXECUTE ( @SqlSelect )

IF ( OBJECT_ID( 'TEMPDB..#ExpressionToDataTypeAndNull' ) IS NOT NULL )
BEGIN
DROP TABLE #ExpressionToDataTypeAndNull
END



Nice Concept,
Doug
SQLRNNR
SQLRNNR
SSC-Dedicated
SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)SSC-Dedicated (33K reputation)

Group: General Forum Members
Points: 33364 Visits: 18560
Nice concept. Thanks for sharing.



Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89439 Visits: 41144
Douglas Osborne-456728 (5/25/2010)
Nick,

This seemed to work a little better for me:


How so? Please describe.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)SSC Guru (89K reputation)

Group: General Forum Members
Points: 89439 Visits: 41144
Bill Nicolich (5/25/2010)
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>.

...

Sorry about that! I'm not sure what happened there.


Bill, send a correction to Steve... he'll get it in. Thanks for taking the time to write this article. Code to write code is one of the essentials to make development a wee bit faster.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Nick Chadwick
Nick Chadwick
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 16
Jeff Moden (5/25/2010)
Douglas Osborne-456728 (5/25/2010)
Nick,

This seemed to work a little better for me:


How so? Please describe.


Doug has added in the "as <column>" on each expression, so that the results have named columns. Nice touch!

Cheers,
Nick
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search