add a temporary column with a value

SqlMysql

Sql Problem Overview


I have a select statement like this

select field1, field2  
  from table1

What I want is to have a newfield with only value "example".

newfield does not exist in the table, but when I run this query it kinda makes a virtual column with the values of example field.

Sql Solutions


Solution 1 - Sql

select field1, field2, 'example' as TempField
from table1

This should work across different SQL implementations.

Solution 2 - Sql

You mean staticly define a value, like this:

SELECT field1, 
       field2,
       'example' AS newfield
  FROM TABLE1

This will add a column called "newfield" to the output, and its value will always be "example".

Solution 3 - Sql

I'm rusty on SQL but I think you could use select as to make your own temporary query columns.

select field1, field2, 'example' as newfield from table1

That would only exist in your query results, of course. You're not actually modifying the table.

Solution 4 - Sql

I giving you an example in wich the TABLE registrofaena doesn't have the column called minutos. Minutos is created and it content is a result of divide demora/60, in other words, i created a column to show the values of the delay in minutes.

This is the query:

SELECT idfaena,fechahora,demora, demora/60 as minutos,comentario 
FROM registrofaena  
WHERE fecha>='2018-10-17' AND comentario <> '' 
ORDER BY idfaena ASC;

This is the view:

This is the result

Solution 5 - Sql

select field1, field2, NewField = 'example' from table1 

Solution 6 - Sql

select field1, field2, '' as newfield from table1

This Will Do you Job

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAsim ZaidiView Question on Stackoverflow
Solution 1 - SqlSteve HomerView Answer on Stackoverflow
Solution 2 - SqlOMG PoniesView Answer on Stackoverflow
Solution 3 - SqlDA.View Answer on Stackoverflow
Solution 4 - SqlDlorkoView Answer on Stackoverflow
Solution 5 - Sqluser372724View Answer on Stackoverflow
Solution 6 - SqlRaj Shekhar SinghView Answer on Stackoverflow