Dustin Geile
Dustin Geile

Reputation: 43

Column Header from Function Result

I have a function that returns dates in a specific format

FormatDate(int PreviousMonths, int Format) returns varchar(100) - E.g. FormatDate(0,2) returns 'June 2011'

But I'm having trouble aliasing the column properly.

A simple example that gives a syntax error is:

Select Foo as dbo.FormatDate(0,2) From FooTable

How can I alias a column with the result of a function?

Sorry, my question seems to be a bit unclear - here is some additional information:

Table named FooTable consisting of one column named Foo, with 3 rows of data containing 1, 2, 3.

Select Foo as dbo.FormatDate(0,2) From FooTable

Returns:
June 2011
1
2
3

Thanks, Dustin

Upvotes: 2

Views: 171

Answers (2)

gbn
gbn

Reputation: 432220

Almost there...

Select Foo = dbo.FormatDate(0,2) From FooTable

Or

Select dbo.FormatDate(0,2) AS Foo From FooTable

Edit:

You can't have dynamic column aliases, especially not per row

You can have the value sent out like this though:

Select
   Foo AS SomeValue,
   dbo.FormatDate(0,2) AS SomeName
From FooTable

Upvotes: 3

zbugs
zbugs

Reputation: 601

Select dbo.FormatDate(0,2) as t from FooTable 

Upvotes: 0

Related Questions