๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
ORACLE/SQL

[ORACLE] ๋ฌธ์ž์—ด ํ•ฉ์น˜๊ธฐ, ๊ธธ์ด ์œ„์น˜ ํ•จ์ˆ˜ CONCAT, INSTR, LENGTH :: ๋งˆ์ด์ž๋ชฝ

by ๐ŸŒปโ™š 2019. 9. 15.

์˜ค๋ผํด ๋ฌธ์ž์—ด ๊ธฐ๋ณธ ํ•จ์ˆ˜ ์‚ฌ์šฉ

์˜ค๋ผํด์—์„œ ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃฐ๋•Œ ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๊ณ , ์œ„์น˜๋ฅผ ํ™•์ธํ•˜๊ณ  ํ•ฉ์น˜๋Š” ํ•จ์ˆ˜๋ฅผ ์•Œ์•„๋ณด์ž.


LENGTH ํ•จ์ˆ˜

๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๊ตฌํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๊ณต๋ฐฑ๋„ ๊ธธ์ด์— ํฌํ•จ๋˜๋ฉฐ ํ•œ๊ธ€ ์บ๋ฆญํ„ฐ์…‹ ์„ธํŒ…์— ๊ด€๊ณ„์—†์ด ํ•œ ๊ธ€์ž๋ฅผ ๊ธธ์ด 1๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค.

๋ช…๋ น : SELECT LENGTH([๋ฌธ์ž์—ด]) FROM [ํ…Œ์ด๋ธ”];


QUERY

1
SELECT LENGTH('MY JAMONG'), LENGTH('๋งˆ์ด์ž๋ชฝ'FROM DUAL;
cs


RESULT




INSTR ํ•จ์ˆ˜

๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ์ˆซ์ž๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋ช‡๋ฒˆ์งธ ๋ฌธ์ž๋ถ€ํ„ฐ ์ฐพ์„์ง€, ๊ทธ๋ฆฌ๊ณ  ๋ช‡๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ฐพ์„์ง€ ์ธ์ž๊ฐ’์œผ๋กœ ์„ค์ •์ด ๊ฐ€๋Šฅํ•˜๋‹ค. ๋งŒ์•ฝ ์ฐพ๋Š” ๋ฌธ์ž๊ฐ€ ์—†๋‹ค๋ฉด '0'์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์˜๋ฌธ ํ•œ๋ฌธ ๋ชจ๋‘ ๊ธ€์ž ๋‹จ์œ„๋กœ ์œ„์น˜๋ฅผ ์•Œ๋ ค์ค€๋‹ค.

๋ช…๋ น : SELECT INSTR([๋ฌธ์ž์—ด], [์ฐพ์„ ๋ฌธ์ž์—ด], [๋ฌธ์ž์—ด์„ ์ฐพ์„ ์‹œ์ž‘ ์œ„์น˜], [์ฐพ์„ ๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ ์œ„์น˜]) FROM [ํ…Œ์ด๋ธ”];


QUERY

1
2
3
4
5
6
7
8
SELECT 
    INSTR('MYJAMONG''M'1)
    ,INSTR('MYJAMONG''M'2)
    ,INSTR('MYJAMONG''M'12)
    ,INSTR('MYJAMONG''M'22)
    ,INSTR('MYJAMONGMY''MY'12)
    ,INSTR('MYJAMONGํ•œ''ํ•œ'51
FROM DUAL;
cs

RESULT

์ฐฌ์„ ๋ฌธ์ž์—ด์˜ n๋ฒˆ์งธ ์œ„์น˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์ œ์™ธ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ œ์™ธํ•˜๊ฒŒ๋˜๋ฉด ์‹œ์ž‘์œ„์น˜๋กœ ๋ถ€ํ„ฐ ์ฒซ๋ฒˆ์งธ ๋ฌธ์ž์—ด์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.





CONCAT ํ•จ์ˆ˜ ๊ทธ๋ฆฌ๊ณ  '||'

CONCATํ•จ์ˆ˜๋Š” ๋‘๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ํ•ฉ์ณ์ฃผ๋Š” ํ•จ์ˆ˜์ด๋‹ค. ๋ฌธ์ž์—ด ๋ฟ๋งŒ์•„๋‹ˆ๋ผ ์ˆซ์žํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ์™€ ํ•จ๊ป˜ ํ•ฉ์น˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค. CONCATํ•จ์ˆ˜๋Š” 2๊ฐœ ๋ฐ์ดํ„ฐ๊นŒ์ง€๋งŒ ํ•ฉ์น ์ˆ˜ ์žˆ์–ด 3๊ฐœ ์ด์ƒ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ•ฉ์น˜๊ณ  ์‹ถ๋‹ค๋ฉด '||' ๋ฌธ์ž๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•ฉ์น ์ˆ˜ ์žˆ๋‹ค.

๋ช…๋ น1 : SELECT CONCAT([๋ฌธ์ž์—ด1], [๋ฌธ์ž์—ด2]) FROM [ํ…Œ์ด๋ธ”];

๋ช…๋ น2 : SELECT [๋ฌธ์ž์—ด1] || [๋ฌธ์ž์—ด2] || [๋ฌธ์ž์—ด3] || ... FROM [ํ…Œ์ด๋ธ”];


QUERY

1
SELECT CONCAT('MY',456), CONCAT('MY','JAMONG'),'123' || 456 || '789' FROM DUAL;
cs

RESULT





๋Œ“๊ธ€