728x90

PostgreSQL ๋ฌธ๋ฒ• 

์˜ˆ์ œ Table - Ticker information

์‹ค์Šต ์˜ˆ์ œ

 

๋”๋ณด๊ธฐ

NOTE

https://www.postgresql.org/docs/current/functions-string.html

 

9.4. String Functions and Operators

9.4. String Functions and Operators 9.4.1. format This section describes functions and operators for examining and manipulating string values. Strings in โ€ฆ

www.postgresql.org

 ๋ฌธ๋ฒ•์— ์•ž์„œ PostgreSQL์— ์‚ฌ์šฉ ๋˜๋Š” ๋ฌธ์ž์—ด ํ•จ์ˆ˜์™€ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ์‹œ

SQL์€ ์‰ผํ‘œ๊ฐ€ ์•„๋‹Œ ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ธ์ˆ˜๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ์ผ๋ถ€ ๋ฌธ์ž์—ด ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ํ‘œ 9.9์— ๋‚˜์™€ ์žˆ์Šต๋‹ˆ๋‹ค. PostgreSQL์€ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜๋Š” ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜์˜ ๋ฒ„์ „๋„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค(ํ‘œ 9.10 ์ฐธ์กฐ).

 

๋ฌธ์ž์—ด ์—ฐ๊ฒฐ ์—ฐ์‚ฐ์ž(||)๋Š” ํ‘œ 9.9์— ํ‘œ์‹œ๋œ ๊ฒƒ์ฒ˜๋Ÿผ ํ•˜๋‚˜ ์ด์ƒ์˜ ์ž…๋ ฅ์ด ๋ฌธ์ž์—ด ์œ ํ˜•์ธ ํ•œ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ํ…์ŠคํŠธ์— ๋ช…์‹œ์  ๊ฐ•์ œ๋ฅผ ์‚ฝ์ž…ํ•˜์—ฌ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด ์•„๋‹Œ ์ž…๋ ฅ์„ ํ—ˆ์šฉํ•จ์œผ๋กœ์จ ์ž์นซ ALL ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ฐ€์ ธ์˜ฌ์ˆ˜ ์žˆ๋‹ค. 

 

 

1) LEFT

left ( string text, n integer ) โ†’ text
	Returns first n characters in the string, or when n is negative, returns all but last |n| characters.
	left('abcde', 2) โ†’ ab

LEFT(๋ฌธ์ž์—ด, ์™ผ์ชฝ๋ถ€ํ„ฐ ์ถ”์ถœ ๋ฌธ์ž์—ด ์ˆ˜)

SELECT LEFT(si.SYMBOL,1) LEFT1
     , LEFT(si.SYMBOL,2) LEFT2
     , LEFT(si.SYMBOL,3) LEFT3
     , LEFT(si.SYMBOL,4) LEFT4
     , LEFT(si.SYMBOL,5) LEFT5
     , si.SYMBOL
  FROM re_stock.STOCK_INFO si
 WHERE si.SYMBOL IN ("AAPL","GOOGL","TSLA","LCID","MSFT","OPEN")
 ORDER BY si.SYMBOL DESC

 

 0์ดํ•˜์˜ ๊ฐ’์€ ๋ชจ๋‘ "" ๋นˆ ๋ฌธ์ž์—ด์„ Returnํ•œ๋‹ค. NULL ๊ฐ’์„ Returnํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ์ฃผ์˜



2) REPLACE

replace ( string text, from text, to text ) โ†’ text
	Replaces all occurrences in string of substring from with substring to.
	replace('abcdefabcdef', 'cd', 'XX') โ†’ abXXefabXXef

REPLACE( ๋ฌธ์ž์—ด, ๋ณ€๊ฒฝ ์ „ ๋ฌธ์ž์—ด, ๋ณ€๊ฒฝ ํ›„ ๋ฌธ์ž์—ด)

SELECT si.SYMBOL SYMBOL
     , REPLACE(si.SYMBOL, "FB", "META") "REPLACE"
  FROM re_stock.STOCK_INFO si
 WHERE si.SYMBOL IN ("FB");

 FB(Facebook)์—์„œ Meta๋กœ ์‚ฌ๋ช…์„ ๋ฐ”๊ฟ” REPLACE ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ

 

 

3) SUBSTRING

/*1*/ 
substring(string [from int] [for int])		text	Extract substring	substring('Thomas' from 2 for 3)	hom
/*2*/ 
substring(string from pattern)				text	Extract substring 	matching POSIX regular expression. See Section 9.7 for more information on pattern matching.	substring('Thomas' from '...$')	mas
/*3*/ 
substring(string from pattern for escape)	text	Extract substring 	matching SQL regular expression. See Section 9.7 for more information on pattern matching.	substring('Thomas' from '%#"o_a#"_' for '#')	oma

1) SUBSTRING(string , n,f) n์ž๋ฆฌ๋ถ€ํ„ฐ f๊นŒ์ง€ ๋ฌธ์ž์—ด ์ถ”์ถœ

2) SUBSTRING(string , 'regex format') n์ž๋ฆฌ๋ถ€ํ„ฐ f๊นŒ์ง€ ๋ฌธ์ž์—ด ์ถ”์ถœ

3) SUBSTRING(string , '#a-b#') escape letter "##"

๋ฐ˜์‘ํ˜•
๋‹คํ–ˆ๋‹ค