• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 810
  • Last Modified:

SQL Case Statement as Alias

Hello experts -

Is it possible to use a case statement as an alias in SQL?  I am trying to print the fiscal year as the column header but need it to dynamically change.  The following statement works on it's own

case when month(GETDATE())>6 then 'FY ' + convert(varchar(4), year(getdate())) else 'FY ' + convert(varchar(4), (year(getdate())-1)) end as Year,

but I would like to put it as the alias - like this

gifts.GiftAmount as case when month(GETDATE())>6 then 'FY ' + convert(varchar(4), year(getdate())) else 'FY ' + convert(varchar(4), (year(getdate())-1)) end,

What am I doing wrong or is what I want to do not possible?

Thank you.

Lisa
0
lcallah93
Asked:
lcallah93
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
no, you cannot "dynamically" inside the query specify the column name.
the expressions are only to evaluate the column values.
0
 
Jared_SCommented:
You could perform a pivot to get the fiscal year column value as a header, but you can't use a case as an alias.
0
 
HainKurtSr. System AnalystCommented:
do that logic on your app/report not in query...
0
 
LIONKINGCommented:
If you're not using data from the query in your case statement, the only way to do it would be to use dynamic sql (with a variable that holds the sql statement and then execute it).
0
 
lcallah93Author Commented:
That is what I have been doing - I was hoping I could accomplish it in the SQL but, alas, it is not to be.

Thanks.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now