SQLite solution for MySQL completion function LPAD and RPAD

        In the work, it is often necessary to clean the data and format individual fields, such as filling left and right strings. The MySQL database comes with LPAD() and RPAD(), but the corresponding functions that the SQLite database does not have, need to be converted by yourself.

Table of contents

1. MySQL database

1.1, MySQL left and right completion function

1.2. Practice verification

2. SQLite database

2.1, SQLite left/right completion processing

2.2. Practice verification

3. Database development client IDE

3.1, DBeaver download and installation

3.2, HeidiSQL download and installation


Operating environment:

1. MySQL database

        In the work, it is often necessary to clean the data and format individual fields. For left and right completion of strings, the default completion functions LPAD() and RPAD() in MySQL can meet the requirements.

1.1, MySQL left and right completion function

  • Left completion function : LPAD (string, length, pad character)
  • Right completion function : RPAD (string, length, pad character)

Example: To output a string with a length of 6 digits, when the length is less than 6 digits , fill the corresponding number of 0s on the left or right

SELECT 
    -- 长度6,不足 左补 0 
    LPAD('数字1', 6, '0'),
    LPAD('数字12', 6, '0'),
    LPAD('数字123', 6, '0'),

    -- 长度6,不足 右补 0 
    RPAD('数字1', 6, '0'),
    RPAD('数字12', 6, '0'),
    RPAD('数字123', 6, '0')
;

1.2. Practice verification

2. SQLite database

The LPAD () and RPAD () completion functions that come with the  MySQL database are         unfortunately not available in SQLite , a lightweight database, and you need to write an alternative solution yourself.

2.1, SQLite left/right completion processing

Example: To output a string with a length of 6 digits, when the length is less than 6 digits , fill the corresponding number of 0s on the left or right

SELECT 
    -- 长度6,不足 左补 0 
    SUBSTR('000000' || '数字1' , -6),
    SUBSTR('000000' || '数字12'  , -6),
    SUBSTR('000000' || '数字123' , -6),

    -- 长度6,不足 右补 0 
    SUBSTR('数字1' || '000000' , 1, 6),
    SUBSTR('数字12'  || '000000' , 1, 6),
    SUBSTR('数字123'  || '000000' , 1, 6)
;

Note: The database character set for this test is UTF-8, and each character or Chinese character occupies 1 bit.

2.2. Practice verification

3. Database development client IDE

3.1, DBeaver download and installation

Open the official website  of DBeaver and download the installation package of the corresponding version of Download DBeaver Community   .

3.2, HeidiSQL download and installation

Open the official website  of HeidiSQL and download the installation package of the corresponding version of Download HeidiSQL   .


appendix:

Guess you like

Origin blog.csdn.net/Sn_Keys/article/details/128507033