SQL Case Statement as Alias

Posted on 2012-08-29
Medium Priority
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 143

Expert Comment

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

Expert Comment

ID: 38345317
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 61

Accepted Solution

HainKurt earned 1500 total points
ID: 38345536
do that logic on your app/report not in query...
LVL 13

Expert Comment

ID: 38345582
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

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


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Suggested Courses

862 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