Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL SELECT multiple columns into one

I have this query in SQL Server 2008:

SELECT Id, Year, Manufacturer, Model  
FROM Table

and I need something like this...

SELECT Id, (Year + [space] + Manufacturer + [space] + Model) AS MyColumn 
FROM Table

How can I get this result?

like image 416
Mario Avatar asked Nov 20 '12 03:11

Mario


People also ask

How do I set the values of two columns in SQL?

Instead, combine + with the function COALESCE and you'll be set. SELECT COALESCE (column1,'') + COALESCE (column2,'') FROM table1. For this example, if column1 is NULL, then the results of column2 will show up, instead of a simple NULL. Hope this helps! select col1 | col 2 as bothcols from tbl ... select col1 + col2 as bothcols from tbl ...

How to select multiple columns and display in a single column in MySQL?

Select multiple columns and display in a single column in MySQL? Select multiple columns and display in a single column in MySQL? Use concat () for this. Let us first create a table − mysql> create table DemoTable -> ( -> FirstName varchar (30), -> LastName varchar (30) -> ); Query OK, 0 rows affected (0.49 sec)

How do I combine two columns in SQL Server?

Instead, combine + with the function COALESCE and you'll be set. SELECT COALESCE (column1,'') + COALESCE (column2,'') FROM table1. For this example, if column1 is NULL, then the results of column2 will show up, instead of a simple NULL. Hope this helps! Yes, you can combine columns easily enough such as concatenating character data:

What are the examples of multiple columns in SQL?

Similarly, another example of multiple columns can be: Write a query which gave the names and salary of all employees working in an organization. So here we have to select 2 columns of name and salary. The examples above make us understand that selection of columns is very important while learning SQL.


2 Answers

I think all integer or numeric data types you need convert to String data type. When you can create your new column.

Query:

SELECT Id, (Cast([Year] as varchar(4)) + ' ' + Manufacturer + ' ' + Model) AS MyColumn 
FROM   Tablename
like image 65
Justin Avatar answered Oct 06 '22 17:10

Justin


just use ' '

SELECT Id, ([Year] + ' ' + Manufacturer + ' ' + Model) AS MyColumn 
FROM   Tablename
like image 7
John Woo Avatar answered Oct 06 '22 18:10

John Woo