What is the correct way to split a string in mysql and get the last string?

tuk :

I have a table which has data like below

mysql> mysql> SELECT * FROM STOCK_COMPANIES LIMIT 10;                                                                
+--------+--------------------------+------------------------+--------------------------------+-----------------------------+                  
| Symbol | Company_name             | Sector                 | Sub_industry                   | Headquarter                 |                  
+--------+--------------------------+------------------------+--------------------------------+-----------------------------+                  
    |    | Agilent Technologies Inc | Health Care            | Health Care Equipment          | Santa Clara; California                        
          |American Airlines Group  | Industrials            | Airlines                       | Fort Worth; Texas                              
          |Advance Auto Parts       | Consumer Discretionary | Automotive Retail              | Roanoke; Virginia                              
      |  | Apple Inc.               | Information Technology | Computer Hardware              | Cupertino; California                          
    |V   | AbbVie                   | Health Care            | Pharmaceuticals                | North Chicago; Illinois                        
 |ABC    | AmerisourceBergen Corp   | Health Care            | Health Care Distributors       | Chesterbrook; Pennsylvania                     
    |    | Abbott Laboratories      | Health Care            | Health Care Equipment          | North Chicago; Illinois                        
            |centure plc            | Information Technology | IT Consulting & Other Services | Dublin; Ireland                                
       | | Adobe Systems Inc        | Information Technology | Application Software           | San Jose; California                           
     |   | Analog Devices; Inc.     | Information Technology | Semiconductors                 | Norwood; Massachusetts                         
+--------+--------------------------+------------------------+--------------------------------+-----------------------------+                  
10 rows in set (0.00 sec)     

I want to split the content of Headquarter by ; and get the last string. I executed the below command

mysql> SELECT Headquarter, SUBSTRING_INDEX(Headquarter, ';', -1) AS STATE  FROM STOCK_COMPANIES LIMIT 10;            
+-----------------------------+-----------------+                                                                    
| Headquarter                 | STATE           |                                                                    
+-----------------------------+-----------------+                                                                    
    |  Californialifornia                                                                                            
         ||  Texass                                                                                                  
      |   |  Virginia                                                                                                
    | |  Californiarnia                                                                                              
      |Illinois; Illinois                                                                                            
  | PennsylvaniaPennsylvania                                                                                         
      |Illinois; Illinois                                                                                            
       |    |  Ireland                                                                                               
    |  |  Californiaia                                                                                               
 |   |  Massachusettstts                                                                                             
+-----------------------------+-----------------+  

Can someone explain to me why the output is looking like the above?

Even if I just try to display the Headquarter it is looking like below

mysql> SELECT Headquarter  FROM STOCK_COMPANIES LIMIT 10;                                                                                      
+-----------------------------+                                                                                                                
| Headquarter                 |                                                                                                                
+-----------------------------+                                                                                                                
    |ta Clara; California                                                                                                                      
          |h; Texas                                                                                                                            
          |Virginia                                                                                                                            
      |tino; California                                                                                                                        
    |th Chicago; Illinois                                                                                                                      
 |Chesterbrook; Pennsylvania                                                                                                                   
    |th Chicago; Illinois                                                                                                                      
            |land                                                                                                                              
       |se; California                                                                                                                         
     |ood; Massachusetts                                                                                                                       
+-----------------------------+                                                                                                                
10 rows in set (0.00 sec) 

I started mysql using -sN and executed the below queries. Both of them giving the expected output

mysql> SELECT Headquarter  FROM STOCK_COMPANIES LIMIT 10;                                                            
Santa Clara; California                                                                                              
Fort Worth; Texas                                                                                                    
Roanoke; Virginia                                                                                                    
Cupertino; California                                                                                                
North Chicago; Illinois                                                                                              
Chesterbrook; Pennsylvania                                                                                           
North Chicago; Illinois                                                                                              
Dublin; Ireland                                                                                                      
San Jose; California                                                                                                 
Norwood; Massachusetts 

mysql> SELECT TRIM(SUBSTRING_INDEX(Headquarter, ';', -1)) AS STATE  FROM STOCK_COMPANIES LIMIT 10;                   
California                                                                                                           
Texas                                                                                                                
Virginia                                                                                                             
California                                                                                                           
Illinois                                                                                                             
Pennsylvania                                                                                                         
Illinois                                                                                                             
Ireland                                                                                                              
California                                                                                                           
Massachusetts 

But if I am trying to execute the below query then again it is giving the below output which I am not able to understand

mysql> SELECT Headquarter, TRIM(SUBSTRING_INDEX(Headquarter, ';', -1)) AS STATE  FROM STOCK_COMPANIES LIMIT 10;      
        Californiaornia                                                                                              
        Texasexas                                                                                                    
        Virginiaa                                                                                                    
        Californiania                                                                                                
        Illinoisllinois                                                                                              
        Pennsylvanialvania                                                                                           
        Illinoisllinois                                                                                              
        Ireland                                                                                                      
        Californiaia                                                                                                 
        Massachusettss       

Can someone explain to me why I am not able to print Headquarter and TRIM(SUBSTRING_INDEX(Headquarter, ';', -1)) together?

Output of mysql -V

$ mysql -V                                                     
mysql  Ver 14.14 Distrib 5.6.39, for Linux (x86_64) using  EditLine wrapper 

OS

$ lsb_release -a                                               
LSB Version:    :core-4.1-amd64:core-4.1-noarch                                                      
Distributor ID: CentOS                                                                               
Description:    CentOS Linux release 7.4.1708 (Core)                                                 
Release:        7.4.1708                                                                             
Codename:       Core  
Barmar :

You have carriage return characters in your data.

I suggest you clean them out of the data and fix your procedures for loading the data in the first place, so you don't have to filter them in all queries:

UPDATE STOCK_COMPANIES
SET headquarter = REPLACE('\r', '', headquarter);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=217007&siteId=1