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?
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 ...
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)
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:
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.
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
just use ' '
SELECT Id, ([Year] + ' ' + Manufacturer + ' ' + Model) AS MyColumn
FROM Tablename
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With