Search for text between delimiters in MySQL

前端 未结 11 2412
情歌与酒
情歌与酒 2021-02-08 03:39

I am trying to extract a certain part of a column that is between delimiters.

e.g. find foo in the following

test \'esf :foo: bar

So in the above I\'d wa

相关标签:
11条回答
  • 2021-02-08 04:05

    This is what I am extracting from (mainly colon ':' as delimiter but some exceptions), as column theline255 in table loaddata255:

    23856.409:0023:trace:message:SPY_EnterMessage (0x2003a) L"{#32769}"      [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0
    

    This is the MySql code (It quickly did what I want, and is straight forward):

    select 
    time('2000-01-01 00:00:00' + interval substring_index(theline255, '.', 1) second) as hhmmss
    , substring_index(substring_index(theline255, ':', 1), '.', -1) as logMilli
    , substring_index(substring_index(theline255, ':', 2), ':', -1) as logTid
    , substring_index(substring_index(theline255, ':', 3), ':', -1) as logType
    , substring_index(substring_index(theline255, ':', 4), ':', -1) as logArea
    , substring_index(substring_index(theline255, ' ', 1), ':', -1) as logFunction
    , substring(theline255, length(substring_index(theline255, ' ', 1)) + 2) as logText
    from loaddata255
    

    and this is the result:

    # LogTime, LogTimeMilli, LogTid, LogType, LogArea, LogFunction, LogText
    '06:37:36', '409', '0023', 'trace', 'message', 'SPY_EnterMessage', '(0x2003a) L\"{#32769}\"      [0081] WM_NCCREATE sent from self wp=00000000 lp=0023f0b0'
    
    0 讨论(0)
  • 2021-02-08 04:07

    With only one set of delimeters, the following should work:

    SUBSTR(
        SUBSTR(fooField,LOCATE(':',fooField)+1),
        1,
        LOCATE(':',SUBSTR(fooField,LOCATE(':',fooField)+1))-1
     )
    
    0 讨论(0)
  • 2021-02-08 04:09

    Here ya go, bud:

    SELECT 
      SUBSTR(column, 
        LOCATE(':',column)+1, 
          (CHAR_LENGTH(column) - LOCATE(':',REVERSE(column)) - LOCATE(':',column))) 
    FROM table
    

    Yea, no clue why you're doing this, but this will do the trick.

    By performing a LOCATE, we can find the first ':'. To find the last ':', there's no reverse LOCATE, so we have to do it manually by performing a LOCATE(':', REVERSE(column)).

    With the index of the first ':', the number of chars from the last ':' to the end of the string, and the CHAR_LENGTH (don't use LENGTH() for this), we can use a little math to discover the length of the string between the two instances of ':'.

    This way we can peform a SUBSTR and dynamically pluck out the characters between the two ':'.

    Again, it's gross, but to each his own.

    0 讨论(0)
  • 2021-02-08 04:11

    If you know the position you want to extract from as opposed to what the data itself is:

    $colNumber = 2; //2nd position
    $sql = "REPLACE(SUBSTRING(SUBSTRING_INDEX(fooField, ':', $colNumber),
                                 LENGTH(SUBSTRING_INDEX(fooField, 
                                                        ':', 
                                                        $colNumber - 1)) + 1)";
    
    0 讨论(0)
  • 2021-02-08 04:13

    A combination of LOCATE and MID would probably do the trick.

    If the value "test 'esf :foo: bar" was in the field fooField:

    MID( fooField, LOCATE('foo', fooField), 3);
    
    0 讨论(0)
提交回复
热议问题