MySQL combine two columns into one column

后端 未结 10 943
后悔当初
后悔当初 2020-11-29 00:46

I\'m trying to find a way to combine two columns into one, but keep getting the value \'0\' in the column instead to the combination of the words.

These are what I\'

相关标签:
10条回答
  • 2020-11-29 01:18

    Try this, it works for me

    select (column1 || ' '|| column2) from table;
    
    0 讨论(0)
  • 2020-11-29 01:22

    For the MySQL fans out there, I like the IFNULL() function. Other answers here suggest similar functionality with the ISNULL() function in some implementations. In my situation, I have a column of descriptions which is NOT NULL, and a column of serial numbers which may be NULL This is how I combined them into one column:

    SELECT CONCAT(description,IFNULL(' SN: ', serial_number),'')) FROM my_table;
    

    My results suggest that the results of concatenating a string with NULL results in a NULL. I have been getting the alternative value in those cases.

    0 讨论(0)
  • 2020-11-29 01:23

    table:

    ---------------------
    | column1 | column2 |
    ---------------------
    |   abc   |   xyz   |
    ---------------------
    

    In Oracle:

    SELECT column1 || column2 AS column3
    FROM table_name;
    

    Output:

    table:

    ---------------------
    | column3           |
    ---------------------
    | abcxyz            |
    ---------------------
    

    If you want to put ',' or '.' or any string within two column data then you may use:

    SELECT column1 || '.' || column2 AS column3
    FROM table_name;
    

    Output:

    table:

    ---------------------
    | column3           |
    ---------------------
    | abc.xyz           |
    ---------------------
    
    0 讨论(0)
  • 2020-11-29 01:32

    If you are Working On Oracle Then:

    SELECT column1 || column2 AS column3
    FROM table;
    

    OR

    If You Are Working On MySql Then:

    SELECT Concat(column1 ,column2) AS column3
    FROM table;
    
    0 讨论(0)
  • 2020-11-29 01:40
    SELECT Collumn1 + ' - ' + Collumn2 AS 'FullName' FROM TableName                              
    
    0 讨论(0)
  • 2020-11-29 01:42

    I have used this way and Its a best forever. In this code null also handled

    SELECT Title,
    FirstName,
    lastName, 
    ISNULL(Title,'') + ' ' + ISNULL(FirstName,'') + ' ' + ISNULL(LastName,'') as FullName 
    FROM Customer
    

    Try this...

    0 讨论(0)
提交回复
热议问题