How to substring a MySQL table column

后端 未结 6 2084
借酒劲吻你
借酒劲吻你 2020-12-25 09:32

I want to select a field from table and substring it.

For example:

VAN1031 --> 1031

I tried this, but is improper syntax:

<
6条回答
  •  南方客
    南方客 (楼主)
    2020-12-25 10:12

    You can use:

    SUBSTR(string,position)
    SUBSTR(string,position,length)
    SUBSTRING_INDEX(string, delimiter, count)
    

    Examples:

    command                                      prints
    -------------------------------------------  -----------
    select substr("abcd", 1, 1)                  #a
    select substr("abcd", 1, 2)                  #ab
    select substr("abcd", 2, 1)                  #b
    select substr("abcd", 2, 2)                  #bc
    select substr("abcd", -2, 1)                 #c
    select substr("abcd", -2, 2)                 #cd
    
    select substring_index('ababab', 'b', 1);    #a
    select substring_index('ababab', 'b', 2);    #aba
    select substring_index('ababab', 'b', 3);    #ababa
    select substring_index('ababab', 'b', -1);   #
    select substring_index('ababab', 'b', -2);   #ab
    select substring_index('ababab', 'b', -3);   #abab
    
    select substr("abcd", 2)                     #bcd
    select substr("abcd", 3)                     #cd
    select substr("abcd", 4)                     #d
    select substr("abcd", -2)                    #cd
    select substr("abcd", -3)                    #bcd
    select substr("abcd", -4)                    #abcd
    

    From this link.

提交回复
热议问题