Using MS SQL Server but porting on MySQL so I need a solution for both ideally (but MySQL preferably)
I am trying to concatenate two field in order to specify the field name for the select statement.
Example table:
id | name
1 | John
2 | Bob
SELECT id, 'na'+'me' as value FROM table WHERE 1=1
actual return (of course)
id | value
1 | name
2 | name
expected return:
id | value
1 | John
2 | Bob
I have no clue as to how to specify that the concatenate result is to be used as field name, and not as result.
I’m confused by your question but the query you need to get the
expected returnis simply:This will work in both mySQL and SQL Server
UPDATE:
It just occured to me that you might be talking about specifying which column to select using concatenated values. To do this in SQL Server use dynamic SQL. Example:
The above would be pointless if that’s how you actually plan on using it. If
naandmeare parameters or something this would make sense. Be sure to read this article before using Dynamic SQL in SQL Server.MySQL Version:
Lifted from here.