rpad 문법은 rpad(A,N,B)
와 같이 썼을 때, 전체 문자열 길이가 N 만큼 되도록 B 문자열을 붙여준다. 때문에 fixed-width 컬럼의 값이나 검색 조건을 만들어내는 데에 사용할 수 있다.
우리 시스템에서는 특정 이벤트를 식별하기 위해서 맨 앞 열 여섯 자리는 해당 이벤트 발생 시간이면서 33 자리인 id를 키로 사용하고 있는데 이 때에 rpad
를 활용하면 유용하다.
특정 시간 대(2020/01/24 09:00 ~ 09:10)의 이벤트들을 조회하고자 하는 경우 다음과 같이 조회해볼 수 있겠다.
where eid >= rpad('202001240900',33,chr(33))
and eid < rpad('202001241000',33,chr(33))
eid 에는 특수문자도 포함될 수 있기 떄문에, printable character 중에서 가장 낮은 값인 chr(33)
('!'
) 를 채워주면 해당 구간의 모든 이벤트를 조회해볼 수 있다.
between
구문을 굳이 사용하려고 한다면 가장 높은 값인 chr(126)
('~'
) 값을 채워주면 된다.
where eid between rpad('202001240900',33,chr(33))
and rpad('202001241000',33,chr(126))
eid
컬럼에 substr 을 이용하는 방식으로 검색할 수도 있겠지만 풀스캔이 일어날 수 있기 때문에 검색 조건에 문자열을 패딩해서 검색하는 것이 인덱스를 제대로 활용할 수 있고 더 나은 방법이다.
아스키 코드 표
다음과 같이 정리가 잘 된 테이블이 있어서 하나 가져와봤다.
Bin. | Hex. | Dec. | Symbol | Explanation |
---|---|---|---|---|
0000000 | 0 | 0 | NUL | The null character prompts the device to do nothing Control Character |
0000001 | 1 | 1 | SOH | Initiates a header (Start of Heading) Control Character |
0000010 | 2 | 2 | STX | Ends the header and marks the beginning of a message. (start of text) Control Character |
0000011 | 3 | 3 | ETX | Indicates the end of the message (end of text) Control Character |
0000100 | 4 | 4 | EOT | Marks the end of a completes transmission (End of Transmission) Control Character |
0000101 | 5 | 5 | ENQ | A request that requires a response (Enquiry) Control Character |
0000110 | 6 | 6 | ACK | Gives a positive answer to the request (Acknowledge) Control Character |
0000111 | 7 | 7 | BEL | Triggers a beep (Bell) Control Character |
0001000 | 8 | 8 | BS | Lets the cursor move back one step (Backspace) Control Character |
0001001 | 9 | 9 | TAB (HT) | A horizontal tab that moves the cursor within a row to the next predefined position (Horizontal Tab) Control Character |
0001010 | A | 10 | LF | Causes the cursor to jump to the next line (Line Feed) Control Character |
0001011 | B | 11 | VT | The vertical tab lets the cursor jump to a predefined line (Vertical Tab) Control Character |
0001100 | C | 12 | FF | Requests a page break (Form Feed) Control Character |
0001101 | D | 13 | CR | Moves the cursor back to the first position of the line (Carriage Return) Control Character |
0001110 | E | 14 | SO | Switches to a special presentation (Shift Out) Control Character |
0001111 | F | 15 | SI | Switches the display back to the normal state (Shift In) Control Character |
0010000 | 10 | 16 | DLE | Changes the meaning of the following characters (Data Link Escape) Control Character |
0010001 | 11 | 17 | DC1 | Control characters assigned depending on the device used (Device Control) Control Character |
0010010 | 12 | 18 | DC2 | Control characters assigned depending on the device used (Device Control) Control Character |
0010011 | 13 | 19 | DC3 | Control characters assigned depending on the device used (Device Control) Control Character |
0010100 | 14 | 20 | DC4 | Control characters assigned depending on the device used (Device Control) Control Character |
0010101 | 15 | 21 | NAK | Negative response to a request (Negative Acknowledge) Control Character |
0010110 | 16 | 22 | SYN | Synchronizes a data transfer, even if no signals are transmitted (Synchronous Idle) Control Character |
0010111 | 17 | 23 | ETB | Marks the end of a transmission block (End of Transmission Block) Control Character |
0011000 | 18 | 24 | CAN | Makes it clear that a transmission was faulty and the data must be discarded (Cancel) Control Character |
0011001 | 19 | 25 | EM | Indicates the end of the storage medium (End of Medium) Control Character |
0011010 | 1A | 26 | SUB | Replacement for a faulty sign (Substitute) Control Character |
0011011 | 1B | 27 | ESC | Initiates an escape sequence and thus gives the following characters a special meaning (Escape) Control Character |
0011100 | 1C | 28 | FS | Marks the separation of logical data blocks and is hierarchically ordered: file as the largest unit, file as the smallest unit.(File Separator, Group Separator, Record Separator, Unit Separator) Control Character |
0011101 | 1D | 29 | GS | Marks the separation of logical data blocks and is hierarchically ordered: file as the largest unit, file as the smallest unit.(File Separator, Group Separator, Record Separator, Unit Separator) Control Character |
0011110 | 1E | 30 | RS | Marks the separation of logical data blocks and is hierarchically ordered: file as the largest unit, file as the smallest unit.(File Separator, Group Separator, Record Separator, Unit Separator) Control Character |
0011111 | 1F | 31 | US | Marks the separation of logical data blocks and is hierarchically ordered: file as the largest unit, file as the smallest unit.(File Separator, Group Separator, Record Separator, Unit Separator) Control Character |
0100000 | 20 | 32 | SP | Blank space (Space) Special Character |
0100001 | 21 | 33 | ! | Exclamation mark Special Character |
0100010 | 22 | 34 | Only quotes above Special Character | |
0100011 | 23 | 35 | # | Pound sign Special Character |
0100100 | 24 | 36 | $ | Dollar sign Special Character |
0100101 | 25 | 37 | % | Percentage sign Special Character |
0100110 | 26 | 38 | & | Commericial and Special Character |
0100111 | 27 | 39 | Apostrophe Special Character | |
0101000 | 28 | 40 | ( | Left bracket Special Character |
0101001 | 29 | 41 | ) | Right bracket Special Character |
0101010 | 2A | 42 | * | Asterisk Special Character |
0101011 | 2B | 43 | + | Plus symbol Special Character |
0101100 | 2C | 44 | , | Comma Special Character |
0101101 | 2D | 45 | – | Dash Special Character |
0101110 | 2E | 46 | . | Full stop Special Character |
0101111 | 2F | 47 | / | Forward slash Special Character |
0110000 | 30 | 48 | 0 | Numbers |
0110001 | 31 | 49 | 1 | Numbers |
0110010 | 32 | 50 | 2 | Numbers |
0110011 | 33 | 51 | 3 | Numbers |
0110100 | 34 | 52 | 4 | Numbers |
0110101 | 35 | 53 | 5 | Numbers |
0110110 | 36 | 54 | 6 | Numbers |
0110111 | 37 | 55 | 7 | Numbers |
0111000 | 38 | 56 | 8 | Numbers |
0111001 | 39 | 57 | 9 | Numbers |
0111010 | 3A | 58 | : | Colon Special characters |
0111011 | 3B | 59 | ; | Semicolon Special characters |
0111100 | 3C | 60 | < | Small than bracket Special characters |
0111101 | 3D | 61 | = | Equals sign Special characters |
0111110 | 3E | 62 | > | Bigger than symbol Special characters |
0111111 | 3F | 63 | ? | Question mark Special characters |
1000000 | 40 | 64 | @ | At symbol Special characters |
1000001 | 41 | 65 | A | Capital letters |
1000010 | 42 | 66 | B | Capital letters |
1000011 | 43 | 67 | C | Capital letters |
1000100 | 44 | 68 | D | Capital letters |
1000101 | 45 | 69 | E | Capital letters |
1000110 | 46 | 70 | F | Capital letters |
1000111 | 47 | 71 | G | Capital letters |
1001000 | 48 | 72 | H | Capital letters |
1001001 | 49 | 73 | I | Capital letters |
1001010 | 4A | 74 | J | Capital letters |
1001011 | 4B | 75 | K | Capital letters |
1001100 | 4C | 76 | L | Capital letters |
1001101 | 4D | 77 | M | Capital letters |
1001110 | 4E | 78 | N | Capital letters |
1001111 | 4F | 79 | O | Capital letters |
1010000 | 50 | 80 | P | Capital letters |
1010001 | 51 | 81 | Q | Capital letters |
1010010 | 52 | 82 | R | Capital letters |
1010011 | 53 | 83 | S | Capital letters |
1010100 | 54 | 84 | T | Capital letters |
1010101 | 55 | 85 | U | Capital letters |
1010110 | 56 | 86 | V | Capital letters |
1010111 | 57 | 87 | W | Capital letters |
1011000 | 58 | 88 | X | Capital letters |
1011001 | 59 | 89 | Y | Capital letters |
1011010 | 5A | 90 | Z | Capital letters |
1011011 | 5B | 91 | [ | Left square bracket Special character |
1011100 | 5C | 92 | \ | Inverse/backward slash Special character |
1011101 | 5D | 93 | ] | Right square bracket Special character |
1011110 | 5E | 94 | ^ | Circumflex Special character |
1011111 | 5F | 95 | _ | Underscore Special character |
1100000 | 60 | 96 | ` | Gravis (backtick) Special character |
1100001 | 61 | 97 | a | Lowercase letters |
1100010 | 62 | 98 | b | Lowercase letters |
1100011 | 63 | 99 | c | Lowercase letters |
1100100 | 64 | 100 | d | Lowercase letters |
1100101 | 65 | 101 | e | Lowercase letters |
1100110 | 66 | 102 | f | Lowercase letters |
1100111 | 67 | 103 | g | Lowercase letters |
1101000 | 68 | 104 | h | Lowercase letters |
1101001 | 69 | 105 | i | Lowercase letters |
1101010 | 6A | 106 | j | Lowercase letters |
1101011 | 6B | 107 | k | Lowercase letters |
1101100 | 6C | 108 | l | Lowercase letters |
1101101 | 6D | 109 | m | Lowercase letters |
1101110 | 6E | 110 | n | Lowercase letters |
1101111 | 6F | 111 | o | Lowercase letters |
1110000 | 70 | 112 | p | Lowercase letters |
1110001 | 71 | 113 | q | Lowercase letters |
1110010 | 72 | 114 | r | Lowercase letters |
1110011 | 73 | 115 | s | Lowercase letters |
1110100 | 74 | 116 | t | Lowercase letters |
1110101 | 75 | 117 | u | Lowercase letters |
1110110 | 76 | 118 | v | Lowercase letters |
1110111 | 77 | 119 | w | Lowercase letters |
1111000 | 78 | 120 | x | Lowercase letters |
1111001 | 79 | 121 | y | Lowercase letters |
1111010 | 7A | 122 | z | Lowercase letters |
1111011 | 7B | 123 | { | Left curly bracket Special characters |
1111100 | 7C | 124 | l | Vertical line Special characters |
1111101 | 7D | 125 | } | Right curly brackets Special characters |
1111110 | 7E | 126 | ~ | Tilde Special characters |
1111111 | 7F | 127 | DEL | Deletes a character. Since this control character consists of the same number on all positions, during the typewriter era it was possible to invalidate another character by punching out all the positions (Delete) Control characters |
임팔라에서 아래와 같이 쿼리해보면 아스키코드 순서대로 정렬되는 것을 확인할 수 있다.
select *
from (select chr(32) as c, 32 as n union all
select chr(33) as c, 33 as n union all
select chr(34) as c, 34 as n union all
select chr(35) as c, 35 as n union all
select chr(36) as c, 36 as n union all
select chr(37) as c, 37 as n union all
select chr(38) as c, 38 as n union all
select chr(39) as c, 39 as n union all
select chr(40) as c, 40 as n
) a
order by c asc