SQL Case Statement as Alias

Posted on 2012-08-29
Last Modified: 2012-08-29
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.

Question by:lcallah93
    LVL 142

    Expert Comment

    by:Guy Hengel [angelIII / a3]
    no, you cannot "dynamically" inside the query specify the column name.
    the expressions are only to evaluate the column values.
    LVL 12

    Expert Comment

    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.
    LVL 51

    Accepted Solution

    do that logic on your app/report not in query...
    LVL 13

    Expert Comment

    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).

    Author Closing Comment

    That is what I have been doing - I was hoping I could accomplish it in the SQL but, alas, it is not to be.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    Having an SQL database can be a big investment for a small company. Hardware, setup and of course, the price of software all add up to a big bill that some companies may not be able to absorb.  Luckily, there is a free version SQL Express, but does …
    Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
    Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
    Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now