Click here to Skip to main content
15,890,670 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I want to concatenate the date to the column name while selecting the data from a table.

E.g.:
SQL
SELECT field1 as newfieldname --for changing the field1 to newfieldname
FROM tablename


I want a new field name with current datetime, so it should be like this newfieldname_10/08/2013. How can we append a datetime to the column name in a SQL select statement?


Thanks,

Ginnas
Posted
Updated 9-Oct-13 4:35am
v2
Comments
RedDk 9-Oct-13 15:50pm    
Not GetDate() concatenation because the "date" of which you speak is more of a date in the data somewhere? If not, then Solution 01 top's your answer.

Are tables currently named as "dates" by any chance?

1 solution

If you mean you want the string "fieldname" then do this.

SQL
SELECT 'fieldname_' + CONVERT(NVARCHAR (50), GetDate())


If you actually mean you want the value out of that field name then do
SQL
SELECT fieldName + CONVERT(NVARCHAR (50), GetDate())
FROM table1 ...
 
Share this answer
 
Comments
Manfred Rudolf Bihy 9-Oct-13 10:39am    
OP wants to change the column name as in his example, but he wants to append the current date. I don't think you can only do that by constructing the SQL statement and then execute it. The part after "as" can only be an identifier, I think.

Cheers!
ZurdoDev 9-Oct-13 10:46am    
In that case the OP will need to use EXEC @sql and build it all dynamically. However, the requirement does not make sense.
Manfred Rudolf Bihy 9-Oct-13 11:04am    
I don't understand it either, why somebody would want to do that. :)

Cheers!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900