0

Please take a look at a table below:

enter image description here

I need "the last part" of "what I have" to be number if it's all number and string if it contains character.

In Excel I've achieved this with the following function (as shown above):

=IFERROR(VALUE(TEXT(D2;"0"));TEXT(D2;"0"))

However I want to do this in mySQL in order to compute more effectively.

I've "floated" somwhere around CASE with CAST or CONVERT and also TRIM functions, but I haven't been able to put up something sensical.

A nice "bonus" would be to extract "the part part" by looking for the last "" character (so first "" from right of the string) but no idea at all how to achieve that.

2
  • Note that one column of an SQL result can only have a single type, so either string or numerical. You can either return all values as strings in a single column, or return strings in one and numbers in another column. If you only want to trim away leading 0s from numbers you can do that with CASE, or UNION, or...
    – JimmyB
    Oct 26, 2017 at 14:59
  • Please provide the CREATE TABLE statements and INSERT statements voor MySQL.. and not a image as data. Oct 26, 2017 at 15:06

3 Answers 3

2

Use SUBSTRING_INDEX.

Query

SELECT 
 CASE 
   WHEN SUBSTRING_INDEX(records.data, '_', -1) > 1 # is int check '00004949' returns 1
   THEN SUBSTRING_INDEX(records.data, '_', -1) + 0 # converts '00004949' to 4949
   ELSE SUBSTRING_INDEX(records.data, '_', -1)
 END
  AS word   
FROM (
  SELECT "TRA_PL_NWL_EMA_NWLY_DAI_000_20170610_IN1_01P002bc" AS DATA
  UNION  
  SELECT "TRA_PL_NWL_EMA_NWLY_DAI_000_2017_0909_JET_00004949" AS DATA

) records 

Result

word      
----------
01P002bc  
4949      
0

Following query will somewhat achieve the task:

SELECT 
case 
when SUBSTRING_INDEX(value, "_", -1) REGEXP('(^[0-9]+$)') 
     then Trim(Leading 0 from SUBSTRING_INDEX(value, "_", -1))
else SUBSTRING_INDEX(value, "_", -1)
end as Value
From yourtable;

Click here for Demo

Hope it helps!

2
  • What about things like 000000ASDF?
    – JimmyB
    Oct 26, 2017 at 15:01
  • @JimmyB I've edited the answer. It should work now. Thnx for the pointing it out. Oct 26, 2017 at 15:15
0

I hope this is what u want. At least it does what you have asked for.

SELECT 
CASE
    WHEN (
            CONVERT(
                substring(
                    txt,
                    LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
                    length(txt)
                )
                , signed integer
            )
        ) = 0
    THEN substring(
            txt,
            LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
            length(txt)
        ) 
    ELSE CONVERT(
                substring(
                    txt,
                    LENGTH(txt) - LOCATE('_', REVERSE(txt))+2,
                    length(txt)
                )
                , signed integer
            )
    END as NUMBER

from test.test

This is my test Table and result of SQL:

txt                         NUMBER
DA_DA_ADAD_ADAD_ADAD_asd123 asd123
DA_DA_ADAD_ADAD_ADAD_000123 123
DA_DA_ADAD_ADAD_ADAD_444    444
DA_DA_ADAD_ADAD_ADAD_bsd123 bsd123
DA_DA_ADAD_ADAD_ADAD_csd123 csd123
DA_DA_ADAD_ADAD_ADAD_dsd123 dsd123

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.