๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/DB๊ธฐ์ดˆ

[SQL] mysql ๋ฌธ์ž์—ด์„ ๋‹ค๋ฃจ๋Š” ํ•จ์ˆ˜ ์ถ”๊ฐ€ ์ •๋ฆฌ

728x90

์„œ๋ธŒ์ŠคํŠธ๋ง ํ•จ์ˆ˜

  • LEFT(): ๋ฌธ์ž์—ด์—์„œ ๋งจ ์™ผ์ชฝ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT LEFT('Hello World', 5); -- Output: Hello
     
  • RIGHT(): ๋ฌธ์ž์—ด์—์„œ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT RIGHT('Hello World', 5); -- Output: World
     
  • SUBSTRING(): ์ง€์ •ํ•œ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜๋Š” ๋ฌธ์ž์—ด์—์„œ ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT SUBSTRING('Hello World', 7); -- Output: World
     
  • SUBSTRING_INDEX(): ์ง€์ •ํ•œ ์œ„์น˜์—์„œ ์‹œ์ž‘ํ•˜์—ฌ ์ง€์ •๋œ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ž์—ด์—์„œ ํ•˜์œ„ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT SUBSTRING_INDEX('www.example.com', '.', 2); -- Output: www.example
     

ํŠธ๋ฆผ ๊ธฐ๋Šฅ

  • TRIM(): ๋ฌธ์ž์—ด์—์„œ ์„ ํ–‰ ๋ฐ ํ›„ํ–‰ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT TRIM(' Hello World '); -- Output: Hello World
     
  • LTRIM(): ๋ฌธ์ž์—ด์—์„œ ์„ ํ–‰ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT LTRIM(' Hello World '); -- Output: Hello World
     
  • RTRIM(): ๋ฌธ์ž์—ด์—์„œ ํ›„ํ–‰ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT RTRIM(' Hello World '); -- Output: Hello World
     

ํ•จ์ˆ˜ ๊ต์ฒด

  • REPLACE(): ์ง€์ •ํ•œ ๋ฌธ์ž์—ด์˜ ๋ชจ๋“  ํ•ญ๋ชฉ์„ ๋‹ค๋ฅธ ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ‰๋‹ˆ๋‹ค.
     
    SELECT REPLACE('Hello World', 'o', 'a'); -- Output: Hella Warld
     

์ผ€์ด์Šค ๋ณ€ํ™˜ ํ•จ์ˆ˜

  • UCASE()/UPPER(): ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT UCASE('Hello World'); -- Output: HELLO WORLD
     
  • LCASE()/LOWER(): ๋ฌธ์ž์—ด์„ ์†Œ๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT LCASE('Hello World'); -- Output: hello world
     
  • INITCAP(): ๋ฌธ์ž์—ด ๋‚ด์˜ ๊ฐ ๋‹จ์–ด์˜ ์ฒซ ๊ธ€์ž๋ฅผ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT INITCAP('hello world'); -- Output: Hello World
     

๋ฌธ์ž์—ด ์กฐ์ž‘ ํ•จ์ˆ˜

  • REPEAT(): ์ง€์ •ํ•œ ํšŸ์ˆ˜๋งŒํผ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณตํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT REPEAT('Hello ', 3); -- Output: Hello Hello Hello
     
  • CONCAT_WS(): ์ง€์ •ํ•œ ๊ตฌ๋ถ„์ž๋กœ ์—ฌ๋Ÿฌ ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT CONCAT_WS('-', '2023', '03', '03'); -- Output: 2023-03-03
     
  • FORMAT(): ์ง€์ •ํ•œ ์†Œ์ˆ˜์  ์ดํ•˜ ์ž๋ฆฟ์ˆ˜์™€ ์ฒœ ๊ตฌ๋ถ„ ๊ธฐํ˜ธ๋กœ ์ˆซ์ž๋ฅผ ํฌ๋งทํ•ฉ๋‹ˆ๋‹ค.
     
    SELECT FORMAT(12345.6789, 2); -- Output: 12,345.68
     
     
728x90