Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


variable column alias


variable column alias

Author
Message
walker31808
walker31808
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 129
Is there a way to use a variable as a column alias?  What I would like to do is produce a column alias of datename(dw,@datefield).  Is there a way to do this?



Adrienne250
Adrienne250
SSC-Enthusiastic
SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)SSC-Enthusiastic (199 reputation)

Group: General Forum Members
Points: 199 Visits: 430

You can use a dynamic query.

Try something like this:

create procedure sp_test
@datefield varchar(20)
as
set nocount on
declare @queryString varchar(200)
set @queryString = 'select '+datename(dw,@datefield)+' = ''a value'''
execute(@queryString)

execute sp_test '07/19/04'
execute sp_test '07/20/04'
execute sp_test '07/21/04'

You should get the alias 'Monday', 'Tuesday' & 'Wednesday' (with 'a value' as the value)





Frank Kalis
Frank Kalis
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5971 Visits: 289

What is your overall goal?

 



--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
walker31808
walker31808
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 129
I am trying to create a report with a column heading of the day of the week but with a column value of the sum of a field for that date.



Frank Kalis
Frank Kalis
SSCertifiable
SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)SSCertifiable (6K reputation)

Group: General Forum Members
Points: 5971 Visits: 289

So, you're doing a crosstab report? Shouldn't the heading be the job of your front-end?

Here is one of the most-referenced tools for this:

http://www.rac4sql.com

Shouldn't be too difficult to combine the result with DATENAME to get what your after.

 



--
Frank Kalis
Microsoft SQL Server MVP
Webmaster: http://www.insidesql.org/blogs
My blog: http://www.insidesql.org/blogs/frankkalis/
walker31808
walker31808
SSC Rookie
SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)SSC Rookie (34 reputation)

Group: General Forum Members
Points: 34 Visits: 129
Thanks for pointing me in the right direction.



David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7961 Visits: 9407

SET DATEFIRST 7  --Set First Day of Week to Sunday

SELECT SUM(CASE WHEN DATEPART(weekday,[date]) = 1 THEN [column] ELSE 0 END) AS 'Sun',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 2 THEN [column] ELSE 0 END) AS 'Mon',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 3 THEN [column] ELSE 0 END) AS 'Tue',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 4 THEN [column] ELSE 0 END) AS 'Wed',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 5 THEN [column] ELSE 0 END) AS 'Thu',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 6 THEN [column] ELSE 0 END) AS 'Fri',
 SUM(CASE WHEN DATEPART(weekday,[date]) = 7 THEN [column] ELSE 0 END) AS 'Sat'
FROM [table]




Far away is close at hand in the images of elsewhere.

Anon.


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