2

I'm looking to do something like this:

select data AS curdate() from table;

so the resulting table would look like:

2013-04-26
  data 1
  data 2
  data 3

I can't figure out the syntax, but it must be possible?

I've tried it without quotes of any kind, which returns an error. Single quotes and back ticks both return the SQL itself as the column header.

1
  • I doubt this is possible
    – Madbreaks
    Apr 26, 2013 at 18:43

2 Answers 2

3

That's an unusual requirement, but if you insist, you'd have to use dynamic sql.

SET @curdate = CURDATE();
SET @sql = CONCAT('SELECT whatever AS "', @curdate, '" FROM whatever');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
3
  • Looks correct to me but when i ran, got this error: Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '2013-04-26 FROM users limit 10' at line 1 (SET @qry = CONCAT('SELECT uid AS ', @curdate, ' FROM users limit 10');)
    – Sumoanand
    Apr 26, 2013 at 19:04
  • @Sumoanand Added ", that should fix this.
    – fancyPants
    Apr 26, 2013 at 19:08
  • Oh, i should have caught it. :) +1
    – Sumoanand
    Apr 26, 2013 at 19:11
0

An alias is considered an identifier and cannot be assigned to a function without the use of dynamic SQL. It would break referencing such as:

SELECT *
FROM (SELECT 1 AS curdate()) a

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.