MySQL combine two columns into one column
My guess is that you are using MySQL where the +
operator does addition, along with silent conversion of the values to numbers. If a value does not start with a digit, then the converted value is 0
.
So try this:
select concat(column1, column2)
Two ways to add a space:
select concat(column1, ' ', column2)
select concat_ws(' ', column1, column2)
concatenate two database columns into one resultset column
The SQL standard way of doing this would be:
SELECT COALESCE(field1, '') || COALESCE(field2, '') || COALESCE(field3, '') FROM table1
Example:
INSERT INTO table1 VALUES ('hello', null, 'world');
SELECT COALESCE(field1, '') || COALESCE(field2, '') || COALESCE(field3, '') FROM table1;
helloworld
how to concatenate multiple column values into a single column?
More than one way to achieve this:
SELECT CONCAT(first_name, ' ' ,last_name) AS full_name;
For earlier versions (Where CONCAT is not a built in function):
SELECT first_name + ISNULL(' ' + last_name, '') as Full_Name from [YourTable]
This as well should give you the same result
SELECT COALESCE(first_name, '') + COALESCE(last_name, '') as FullName FROM [YourTable]
Combining Multiple Columns into One based off another field
Use this :
Select MyId,Col1 as CombininedCol from YourTable where Col1 is not null
Union All
Select MyId,Col2 as CombininedCol from YourTable where Col2 is not null
Union All
Select MyId,Col3 as CombininedCol from YourTable where Col3 is not null
Union All
Select MyId,Col4 as CombininedCol from YourTable where Col4 is not null
order by MyId
Tip: The UNION ALL command combines the result set of two or more SELECT statements (allows duplicate values).
Each select separately get the two columns of MyID and the Cole 1,2, etc., and finally returns them as two columns.
how to concat two columns into one with the existing column name in mysql?
As aziz-shaikh has pointed out, there is no way to suppress an individual column from the *
directive, however you might be able to use the following hack:
SELECT CONCAT(c.FIRSTNAME, ',', c.LASTNAME) AS FIRSTNAME,
c.*
FROM `customer` c;
Doing this will cause the second occurrence of the FIRSTNAME
column to adopt the alias FIRSTNAME_1
so you should be able to safely address your customised FIRSTNAME
column. You need to alias the table because *
in any position other than at the start will fail if not aliased.
Hope that helps!
How to concat multiple columns in sql
SELECT CONCAT(EMP_HOUSE_NO,' ', EMP_STREET_NAME,' ', EMP_AREA) AS
addressfrom EMPLOYEE_DETAILS
WHERE EMP_ID=1;
Concatenate Two columns data into new column
You may try this. As you said your current output is already coming, then only need to add a column by combining 2 column values.
select UserID_FK, DesireMinSalary,DesireMaxSalary, (cast(DesireMinSalary as varchar(15)) + '-' + cast(DesireMaxSalary as varchar(15)) as ConcatSalrye , Answer,
case when answer = CONVERT(VARCHAR(2), DesireMinSalary) then 'True'else 'False' end as reslut
from TAL_TBL_Candidate
inner join tal_tbl_users
on Userid = UserID_FK
inner join [AocJobs_Migration].[dbo].[AoCJobsJobseekerProfiles]
on email = susername
WHERE ProfileQuestion='What is your desired annual salary?'
Combine two columns and input the result in a different columns using SQL server
Firstly, CONCAT was introduced in SQL Server 2012
Using CONCAT function:
SELECT CONCAT ( 'Welcome ', 'World ', '!' ) AS Result;
Secondly, you want auto generated value to be concatenated to the id value for the sample Id column. The below query can be used for that...
SELECT IDENT_CURRENT('table_name')+1;
Now, alter your query as below
SqlCommand sc = new SqlCommand(@"insert into Sample (MBID, SampleType,SampleDate,ConsultantName,Comments,FirstSample, SampleID)
values(@MBID , @SampleType , @SampleDate , @ConsultantName , @Comments, cast((IDENT_CURRENT('Sample')+1) as VARCHAR(max)) +'-'+ CAST(@MBID AS VARCHAR(10)));", con);
Merge data from columns into one column
If you want them in one column, then concatenate them together:
SELECT (dbo.dealer_program_details.o_comments + dbo.Self_Am.o_comments +
dbo.Coop_Payment_Detail.o_comments
) as OneColumn
FROM dbo.dealer_program_details INNER JOIN
dbo.Self_Am
ON dbo.dealer_program_details.DEALER_CODE = dbo.Self_Am.Dealer_Code INNER JOIN
dbo.Coop_Payment_Detail
ONdbo.dealer_program_details.DEALER_CODE = dbo.Coop_Payment_Detail.Dealer_Code;
In Sybase, you don't have to worry about NULL
values, because these are treated as empty strings. In SQL Server, the result will be NULL
if any column values are NULL
.
EDIT:
You can choose the first non-NULL row, by using coalesce()
:
SELECT coalesce(dbo.dealer_program_details.o_comments, dbo.Self_Am.o_comments,
dbo.Coop_Payment_Detail.o_comments
) as OneColumn
But if two (or more) columns have comments, then you will only keep the first non-NULL one.
How to concatenate several columns into single vector in R using apply() family
Maybe just unlist()
will do.
as.numeric(unlist(df[2:5]))
# [1] NA NA NA -0.272 -0.315 -0.628 -0.106 0.428 -0.778 -1.294 -0.780 0.012
# [13] -0.152 -0.703 1.189 0.341 NA 0.507 -0.293 0.224 2.007 1.012 NA NA
# [25] -0.302 -1.025 -0.267 -0.199 0.131 0.146 0.362 NA 0.674 2.072 -0.541 -1.070
# [37] -0.372 -0.485 0.275 -0.480
Related Topics
How to Avoid Duplicate Values for Insert in SQL
"Operator Does Not Exist: Integer =" When Using Postgres
Record Returned from Function Has Columns Concatenated
SQL to Find the Number of Distinct Values in a Column
How to Query for All Dates Greater Than a Certain Date in SQL Server
What Datatype to Use When Storing Latitude and Longitude Data in SQL Databases
How to Check the Maximum Number of Allowed Connections to an Oracle Database
How to Show All Privileges from a User in Oracle
When How to Save JSON or Xml Data in an SQL Table
How to Sum Two Fields Within an SQL Query
In SQL Server, What Does "Set Ansi_Nulls On" Mean
Efficiently Convert Rows to Columns in SQL Server
Get the Records of Last Month in SQL Server
How to Select SQL Server Data Using Column Ordinal Position
Finding Similar Strings with Postgresql Quickly
How to Calculate a Running Total in SQL Without Using a Cursor