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 "##"
'Database' 카테고리의 다른 글
[WSL] ssh: connect to host localhost port 22: Connection refused (0) | 2023.10.29 |
---|---|
[SQL]Window 함수 First_value/Last_value 활용하기 (0) | 2023.08.26 |
[SQL] LIKE (0) | 2023.08.20 |
[SQL] NULLIF, NVL(COALESCE) 함수 (0) | 2023.08.19 |
[SQL] Count 함수, Sequel vs SQL (0) | 2023.08.19 |