1

I'm writing an SQL expression and I'd like to use the current month as the column name/header.

Code:

Select MONTH(GETDATE()) AS MONTH(GETDATE())
FROM SomeTable;

Error:

Error 102: Incorrect syntax near 'GETDATE'.

This is for a school project and I'm not sure if it's possible. If it is, I'd like to possibly convert that Month number to the actual month name. Thanks in advance.

Oh, and I'm using LinqPad to test the queries on a remote DB and SQL Express Server (Transact-SQL).

Cheers, Lindsay

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Lifes
  • 1,226
  • 2
  • 25
  • 45
  • Why would you like to return the alias' name. You may set the column header as you like on the front end UI on whichever language you are using... – Naveed Butt Mar 25 '11 at 06:39

2 Answers2

0

I think, You can not use function in column alias, if you try to then you get this error incorrect syntex "Expecting ID, QUOTED_ID, STRING, or TEXT_LEX" which means the alias text has to be hard coded.

I would suggest, you use your front end application to set current month as header, instead of relying on back end sql query.

Nikhil Vaghela
  • 920
  • 2
  • 11
  • 36
  • She didn't want to get the name of the date in the column itself. She wanted it as alias – Naveed Butt Mar 25 '11 at 06:43
  • I want to use the value of that expression (the current month) as the column header itself. E.g. March. The current month changes so I can't use a hard coded value. – Lifes Mar 25 '11 at 07:03
  • Alright, I figured it was a long shot...no harm in asking. Thanks for all the replies. I'll just code it in the business logic. – Lifes Mar 26 '11 at 05:48
0

The alias for your computed columns shouldn't contain any function - just text:

SELECT
   MONTH(GETDATE()) AS 'Month'
FROM 
   dbo.SomeTable
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459