본문 바로가기

DB스토리/POSTGRESQL

PostgreSQL 문자열 함수와 연산자

표 9-5. SQL 문자열 함수와 연산자

함수반환 값 형식설명 예제結果결과
string || stringtext문자열 결합'Post' || 'greSQL'PostgreSQL
문자열 || 비 문자열 또는 비 문자열 || 문자열text하나가 비문자열인 입력 문자열 결합'Value: ' || 42Value: 42
bit_length( string )int문자열의 비트수bit_length('jose')32
char_length( string ) 또는 character_length( string )int문자열의 문자 수char_length('jose')4
lower( string )text문자열을 소문자로 변환lower('TOM')tom
octet_length( string )int문자열의 바이트 수octet_length('jose')4
overlay( string placing stringfrom int [ for int ])text부분 문자열 대체overlay('Txxxxas' placing 'hom' from 2 for 4)Thomas
position( substring in string )int지정된 부분 문자열의 위치position('om' in 'Thomas')3
substring( string [ from int ] [ for int ])text부분 문자열 추출substring('Thomas' from 2 for 3)hom
substring( string from pattern)textPOSIX 정규식에 일치하는 하위 문자열을 추출합니다. 패턴 일치에 대해 더 자세한 내용은 섹션 9.7 을 참조하십시오.substring('Thomas' from '...$')mas
substring( string from patternfor escape )textSQL 정규 표현에 일치하는 부분 문자열을 검색합니다. 패턴 일치에 대해 더 자세한 내용은 섹션 9.7 을 참조하십시오.substring('Thomas' from '%#"o_a#"_' for '#')oma
trim([ leading | trailing | both ] [ characters ] from string)textcharacters (기본적으로 공백)으로 지정된 문자만 포함하여 가장 긴 문자열을, string의 시작, 끝, 그리고 그 모두 제거합니다.trim(both 'x' from 'xTomxx')Tom
upper( string )text문자열을 대문자로 변환upper('tom')TOM

 

 이밖에 표 9-6 에 열거하는 문자열 조작 함수를 사용할 수 있습니다. 그 일부는 표 9-5 에 나열된 표준 SQL 문자열 함수를 구현하기 위해 내부적으로 사용됩니다.

 

표 9-6. 기타 문자열 함수

함수반환 값 형식설명예제결과
ascii( string )int인수의 첫 문자의 ASCII 코드. UTF8 대해서는 문자의 Unicode 코드 포인트를 반환합니다. 기타 멀티 바이트 encode 방식의 경우, 인수는 ASCII 문자로하지 않으면 안됩니다.ascii('x')120
btrim( string text [characters text ])textcharacters (기본값은 공백 문자)로 지정된 문자만 있는 긴 문자열을 string의 시작과 끝에서 제거합니다.btrim('xyxtrimyyx', 'xy')trim
chr( int )text주어진 코드의 문자. UTF8에서는 인수는 Unicode 코드 포인트로 처리됩니다. 기타 멀티 바이트 인코딩 방식으로 인수는 ASCII 문자이어야합니다. NULL (0) 문자는 텍스트 데이터 형식이 그런 바이트를 저장할 수 없기 때문에 허용되지 않습니다.chr(65)A
convert( string bytea , src_encoding name , dest_encoding name )bytea문자열을 dest_encoding으로 변환합니다. 원래 encode 방식은 src_encoding으로 지정됩니다. string은 이 encode 방식으로 유효해야합니다. 변환은 CREATE CONVERSION에서 정의됩니다. 유효한 변환에 대해서는 표 9-7 을 참조하십시오.convert('text_in_utf8', 'UTF8', 'LATIN1')Latin-1(ISO 8859-1)encode 방식의text_in_utf8
convert_from( stringbytea , src_encodingname )text데이터베이스의 encode 방식으로 문자열을 변환합니다. 원래 encode 방식은 src_encoding으로 지정됩니다. string은 이 encode 방식으로 유효해야합니다.convert_from('text_in_utf8', 'UTF8')현재 데이터베이스 encode 방식의text_in_utf8
convert_to( string text , dest_encodingname )bytea문자열을 dest_encoding으로 변환합니다.convert_to('some text', 'UTF8')UTF8 인코딩 방식의some text
decode( string texttype text )bytea이전에 encode() 으로 부호화된 string에서 이진 데이터를 해독합니다. 매개 변수 형식은 encode() 와 동일합니다.decode('MTIzAAE=', 'base64')123\000\001
encode( data bytea ,type text)text이진 데이터를 다른 표현 방식으로 encode합니다. 지원되는 형식은 base64, hex, escape입니다. escape는 단순히 null 바이트를 \000과 같이 출력하고, backslash를 이중화합니다.encode(E'123\\000\\001', 'base64')MTIzAAE=
initcap( string )text각 단어의 첫 글자를 대문자로 나머지는 소문자로 둡니다. 여기 단어는 영숫자 이외의 문자로 구분된 영숫자 문자 시퀀스입니다.initcap('hi THOMAS')Hi Thomas
length( string )intstring의 문자 수length('jose')4
length( string byteaencodingname )int주어진 encoding에서 string의 문자 수. string이 encode 방식으로 유효해야합니다.length('jose', 'UTF8')4
lpad( string text ,length intfill text ])text문자 fill (기본값은 공백)을 문자열 앞에 추가하여 string을 length 길이와 맞춥니다. string이 이미 length 길이를 초과하는 경우 (오른쪽이) 잘립니다.lpad('hi', 5, 'xy')xyxhi
ltrim( string text [characters text ])textstring의 처음부터, characters (기본값은 공백)로 지정된 문자만을 가장 긴 문자열에서 삭제합니다.ltrim('zzzytrim', 'xyz')trim
md5( string )textstring의 MD5 해시를 계산하여 16 진수로 결과를 반환합니다.md5('abc')900150983cd24fb0 d6963f7d28e17f72
pg_client_encoding()name현재 클라이언트의 encode 방식의 이름입니다.pg_client_encoding()SQL_ASCII
quote_ident( stringtext )text주어진 문자열을 SQL 쿼리 문자열 식별자로 사용할 수 있도록 적절한 인용 부호를 붙여 반환합니다. 인용부는 필요한 경우 (예를 들어, 문자열에 식별자로 사용할 수 없는 문자가 포함된 경우 또는 대문자 변환되는 경우)에만 추가됩니다. 포함된 인용부가 올바르게 이중화됩니다. 예 39-1 을 참조하십시오.quote_ident('Foo bar')"Foo bar"
quote_literal( string text )text与えられた文字列を、 SQL問い合わせ文字列で文字リテラルとして使用できるように、適切な引用符を付けて返します。 주어진 문자열을 SQL 쿼리 문자열에서 문자 리터럴로 사용할 수 있도록 적절한 인용 부호를 붙여 반환합니다. 埋め込まれた単一引用符およびバックスラッシュは、適切に二重化されます。 포함된 작은 따옴표와 백슬래시는 적절하게 이중화됩니다. quote_literalはNULL入力に対してNULLを返すことに注意してください。 quote_literal 은 NULL 입력에 NULL를 돌려주는 것에주의하십시오. 引数がNULLとなる可能性がある場合、よりquote_nullableの方がしばしば適しています。 인수가 NULL이 될 가능성이있는 경우 더 quote_nullable 더 자주 적합합니다. 例39-1も参照してください。 예 39-1 을 참조하십시오.quote_literal('O\'Reilly')'O''Reilly'
quote_literal( valueanyelement)text与えられた値をテキストに強制し、そしてリテラルとして引用符付けします。 주어진 값을 텍스트로 강제하고 리터럴로 인용 부호합니다. 組み込まれた一重引用符と逆スラッシュは適切に二重化されます。 포함된 작은 따옴표 반대 슬래시 제대로 이중화됩니다.quote_literal(42.5)'42.5'
quote_nullable( string text )text주어진 문자열을 SQL 쿼리 문자열에서 문자 리터럴로 사용할 수 있도록 적절한 인용 부호를 붙여 반환합니다. 또한 인수가 NULL인 경우, NULL을 반환합니다. 포함된 작은 따옴표와 백슬래시는 적절히 이중화됩니다. 예 39-1 을 참조하십시오.quote_nullable(NULL)NULL
quote_nullable( value anyelement )text주어진 값을 텍스트로 강제하고, 리터럴로서 인용 부호를 더합니다. 그렇지 않고, 인수가 NULL인 경우는 NULL을 반환합니다. 포함된 작은 따옴표와 백슬래시는 적절히 이중화됩니다.quote_nullable(42.5)'42.5'
regexp_matches( string text ,pattern text [, flags text ])setof text[]string에 대해 POSIX 정규식으로 매치하여, 캡처된 모든 부분 문자열을 반환합니다. 보다 자세한 내용은섹션 9.7.3 을 참조하십시오.regexp_matches('foobarbequebaz', '(bar)(beque)'){bar,beque}
regexp_replace( string text ,pattern text , replacement text [, flagstext ])textPOSIX 정규 표현식과 일치하는 부분 문자열을 대체합니다. 보다 자세한 내용은 섹션 9.7.3 을 참조하십시오.regexp_replace('Thomas', '.[mN]a.', 'M')ThM
regexp_split_to_array( stringtext , pattern text[, flags text ])text[]POSIX 정규식을 구분 기호로 사용하여 string을 분할합니다. 자세한 내용은 섹션 9.7.3 을 참조하십시오.regexp_split_to_array('hello world', E'\\s+'){hello,world}
regexp_split_to_table( stringtext , pattern text[, flags text ])setof textPOSIX 정규식을 구분 기호로 사용하여 string을 분할합니다. 자세한 내용은 섹션 9.7.3 을 참조하십시오.regexp_split_to_table('hello world', E'\\s+')hello

world

(2 rows)

repeat( string textnumber int )text지정된 number 수만큼 string을 반복합니다.repeat('Pg', 4)PgPgPgPg
replace( string textfrom text , to text )textstring에 출현하는 모든 from 부분 문자열을 to 부분 문자열로 대체합니다.replace('abcdefabcdef', 'cd', 'XX')abXXefabXXef
rpad( string text ,length intfill text ])text문자 fill (기본 공간)을 문자열에 추가하고, string을 length 길이로 만듭니다. string 이미 length 길이를 초과하는 경우 잘립니다.rpad('hi', 5, 'xy')hixyx
rtrim( string text [characters text ])textstring의 끝에서부터 characters (기본 공간)에서 지정된 문자만을 유요한 가장 긴 문자열을 삭제합니다.rtrim('trimxxxx', 'x')trim
split_part( string text , delimiter textfield int )textstring을 delimiter로 분할하여 그 결과 (1부터 시작하도록 셈) 지정된 필드를 반환합니다.split_part('abc~@~def~@~ghi', '~@~', 2)def
strpos( string , substring )int지정된 부분 문자열의 위치를 식별합니다 (position (substring in string)과 동일하지만, 인수의 순서가 반대로 되어 있는 것에 주의하십시오).strpos('high', 'ig')2
substr( string , from [ count ])text지정된 부분 문자열을 추출합니다 (substring (string from from for count)와 같습니다).substr('alphabet', 3, 2)ph
to_ascii(string text [ encoding text])textstring을 다른 encode 방식에서 ASCII로 변환합니다. LATIN1, LATIN2, LATIN9, WIN1250 encode 방식의 변환을 지원합니다.  )to_ascii('Karel')Karel
to_hex( number int or bigint )textnumber를 동등한 16 진수 표현으로 변환합니다.to_hex(2147483647)7fffffff
translate(string text , from text , to text )textfrom 집합에서 지정된 문자와 일치하는 string의 모든 문자는 해당하는 to에서 지정된 문자로 대체됩니다.translate('12345', '14', 'ax')a23x5

섹션 9.18 의 string_agg 집계 함수를 참조하십시오.

 

표 9-7 기본 제공 변환

변환 이름[a]변환 전의 encode 방식변환 후의 encode 방식
ascii_to_micSQL_ASCIIMULE_INTERNAL
ascii_to_utf8SQL_ASCIIUTF8
big5_to_euc_twBIG5EUC_TW
big5_to_micBIG5MULE_INTERNAL
big5_to_utf8BIG5UTF8
euc_cn_to_micEUC_CNMULE_INTERNAL
euc_cn_to_utf8EUC_CNUTF8
euc_jp_to_micEUC_JPMULE_INTERNAL
euc_jp_to_sjisEUC_JPSJIS
euc_jp_to_utf8EUC_JPUTF8
euc_kr_to_micEUC_KRMULE_INTERNAL
euc_kr_to_utf8EUC_KRUTF8
euc_tw_to_big5EUC_TWBIG5
euc_tw_to_micEUC_TWMULE_INTERNAL
euc_tw_to_utf8EUC_TWUTF8
gb18030_to_utf8GB18030UTF8
gbk_to_utf8GBKUTF8
iso_8859_10_to_utf8LATIN6UTF8
iso_8859_13_to_utf8LATIN7UTF8
iso_8859_14_to_utf8LATIN8UTF8
iso_8859_15_to_utf8LATIN9UTF8
iso_8859_16_to_utf8LATIN10UTF8
iso_8859_1_to_micLATIN1MULE_INTERNAL
iso_8859_1_to_utf8LATIN1UTF8
iso_8859_2_to_micLATIN2MULE_INTERNAL
iso_8859_2_to_utf8LATIN2UTF8
iso_8859_2_to_windows_1250LATIN2WIN1250
iso_8859_3_to_micLATIN3MULE_INTERNAL
iso_8859_3_to_utf8LATIN3UTF8 UTF8
iso_8859_4_to_micLATIN4MULE_INTERNAL
iso_8859_4_to_utf8LATIN4UTF8
iso_8859_5_to_koi8_rISO_8859_5KOI8R
iso_8859_5_to_micISO_8859_5MULE_INTERNAL
iso_8859_5_to_utf8ISO_8859_5UTF8
iso_8859_5_to_windows_1251ISO_8859_5WIN1251
iso_8859_5_to_windows_866ISO_8859_5WIN866
iso_8859_6_to_utf8ISO_8859_6UTF8
iso_8859_7_to_utf8ISO_8859_7UTF8
iso_8859_8_to_utf8ISO_8859_8UTF8
iso_8859_9_to_utf8LATIN5UTF8
johab_to_utf8JOHABUTF8
koi8_r_to_iso_8859_5KOI8RISO_8859_5
koi8_r_to_micKOI8RMULE_INTERNAL
koi8_r_to_utf8KOI8RUTF8
koi8_r_to_windows_1251KOI8RWIN1251
koi8_r_to_windows_866KOI8RWIN866
koi8_u_to_utf8KOI8UUTF8
mic_to_asciiMULE_INTERNALSQL_ASCII
mic_to_big5MULE_INTERNALBIG5
mic_to_euc_cnMULE_INTERNALEUC_CN
mic_to_euc_jpMULE_INTERNALEUC_JP
mic_to_euc_krMULE_INTERNALEUC_KR
mic_to_euc_twMULE_INTERNALEUC_TW
mic_to_iso_8859_1MULE_INTERNALLATIN1
mic_to_iso_8859_2MULE_INTERNALLATIN2
mic_to_iso_8859_3MULE_INTERNALLATIN3
mic_to_iso_8859_4MULE_INTERNALLATIN4
mic_to_iso_8859_5MULE_INTERNALISO_8859_5
mic_to_koi8_rMULE_INTERNALKOI8R
mic_to_sjisMULE_INTERNALSJIS
mic_to_windows_1250MULE_INTERNALWIN1250
mic_to_windows_1251MULE_INTERNALWIN1251
mic_to_windows_866MULE_INTERNALWIN866
sjis_to_euc_jpSJISEUC_JP
sjis_to_micSJISMULE_INTERNAL
sjis_to_utf8SJISUTF8
tcvn_to_utf8WIN1258UTF8
uhc_to_utf8UHCUTF8
utf8_to_asciiUTF8SQL_ASCII
utf8_to_big5UTF8BIG5
utf8_to_euc_cnUTF8EUC_CN
utf8_to_euc_jpUTF8EUC_JP
utf8_to_euc_krUTF8EUC_KR
utf8_to_euc_twUTF8EUC_TW
utf8_to_gb18030UTF8GB18030
utf8_to_gbkUTF8GBK
utf8_to_iso_8859_1UTF8LATIN1
utf8_to_iso_8859_10UTF8LATIN6
utf8_to_iso_8859_13UTF8LATIN7
utf8_to_iso_8859_14UTF8LATIN8
utf8_to_iso_8859_15UTF8LATIN9
utf8_to_iso_8859_16UTF8LATIN10
utf8_to_iso_8859_2UTF8LATIN2
utf8_to_iso_8859_3UTF8LATIN3
utf8_to_iso_8859_4UTF8LATIN4
utf8_to_iso_8859_5UTF8ISO_8859_5
utf8_to_iso_8859_6UTF8ISO_8859_6
utf8_to_iso_8859_7UTF8ISO_8859_7
utf8_to_iso_8859_8UTF8ISO_8859_8
utf8_to_iso_8859_9UTF8LATIN5
utf8_to_johabUTF8JOHAB
utf8_to_koi8_rUTF8KOI8R
utf8_to_koi8_uUTF8KOI8U
utf8_to_sjisUTF8SJIS
utf8_to_tcvnUTF8WIN1258
utf8_to_uhcUTF8UHC
utf8_to_windows_1250UTF8WIN1250
utf8_to_windows_1251UTF8WIN1251
utf8_to_windows_1252UTF8WIN1252
utf8_to_windows_1253UTF8WIN1253
utf8_to_windows_1254UTF8WIN1254
utf8_to_windows_1255UTF8WIN1255
utf8_to_windows_1256UTF8WIN1256
utf8_to_windows_1257UTF8WIN1257
utf8_to_windows_866UTF8WIN866
utf8_to_windows_874UTF8WIN874
windows_1250_to_iso_8859_2WIN1250LATIN2
windows_1250_to_micWIN1250MULE_INTERNAL
windows_1250_to_utf8WIN1250UTF8
windows_1251_to_iso_8859_5WIN1251ISO_8859_5
windows_1251_to_koi8_rWIN1251KOI8R
windows_1251_to_micWIN1251MULE_INTERNAL
windows_1251_to_utf8WIN1251UTF8
windows_1251_to_windows_866WIN1251WIN866
windows_1252_to_utf8WIN1252UTF8
windows_1256_to_utf8WIN1256UTF8
windows_866_to_iso_8859_5WIN866ISO_8859_5
windows_866_to_koi8_rWIN866KOI8R
windows_866_to_micWIN866MULE_INTERNAL
windows_866_to_utf8WIN866UTF8
windows_866_to_windows_1251WIN866WIN
windows_874_to_utf8WIN874UTF8
euc_jis_2004_to_utf8EUC_JIS_2004UTF8
ut8_to_euc_jis_2004UTF8EUC_JIS_2004
shift_jis_2004_to_utf8SHIFT_JIS_2004UTF8
ut8_to_shift_jis_2004UTF8SHIFT_JIS_2004
euc_jis_2004_to_shift_jis_2004EUC_JIS_2004SHIFT_JIS_2004
shift_jis_2004_to_euc_jis_2004SHIFT_JIS_2004EUC_JIS_2004
주의 : 
a. 변환 이름은 표준 이름 지정 스키마에 따릅니다. 원본 encode 방식의 공식 이름에 영숫자 이외의 문자를 밑줄로 대체 후에 _to_ 를 붙이고, 대상 encode 방식 이름을 같이 처리한 것을 붙인 것입니다. 따라서 이러한 명칭은 평범한 코드 이름과 다를 수 있습니다.


'DB스토리 > POSTGRESQL' 카테고리의 다른 글