<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 9.4\. 字符串函數和操作符 本節描述了用于檢查和操作字符串數值的函數和操作符。 在這個環境中的字符串包括`character`,`character varying`, `text`類型的值。除非另外說明,所有下面列出的函數都可以處理這些類型, 不過要小心的是,在使用`character`類型的時候,需要注意自動填充的潛在影響。 有些函數還可以處理位串類型。 SQL定義了一些字符串函數,用特定的關鍵字而不是逗號來分隔參數。 詳情請見[Table 9-5](#calibre_link-1044)。PostgreSQL 也提供了使用正常的函數調用語法實現的這些函數的版本(參閱 [Table 9-6](#calibre_link-2069))。 > **Note:** 在PostgreSQL 8.3之前, 這些函數將默默接受一些非字符串數據類型的值,由于存在從這些數據類型到`text` 的隱式強制轉換,轉換后的它們經常發生意外的行為,因此刪除了隱式強制轉換。 然而,字符串連接操作符(`||`)仍接受非字符串輸入, 只要至少有一個輸入是字符串類型,如[Table 9-5](#calibre_link-1044)所示。 對于其它情況下,如果你需要重復以前的行為,插入一個明確的強制轉換到`text`。 **Table 9-5\. SQL 字符串函數和操作符** | 函數 | 返回類型 | 描述 | 示例 | 結果 | | --- | --- | --- | --- | --- | | `string` `&#124;&#124;` `string` | `text` | 字符串連接 | `'Post' &#124;&#124; 'greSQL'` | `PostgreSQL` | | `string` `&#124;&#124;` `non-string` 或 `non-string` `&#124;&#124;` `string` | `text` | 帶有一個非字符串輸入的字符串連接 | `'Value: ' &#124;&#124; 42` | `Value: 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 `string` from `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_`) | `text` | 截取匹配POSIX正則表達式的子字符串。參閱[Section 9.7](#calibre_link-1400) 獲取更多關于模式匹配的信息。 | `substring('Thomas' from '...$')` | `mas` | | ``substring(```string` from `_pattern_` for `_escape_`) | `text` | 截取匹配SQL正則表達式的子字符串。 參閱[Section 9.7](#calibre_link-1400)獲取更多關于模式匹配的信息。 | `substring('Thomas' from '%#"o_a#"_' for '#')` | `oma` | | ``trim([leading &#124; trailing &#124; both] [```characters`] from `string`) | `text` | 從字符串`string`的開頭/結尾/兩邊刪除只包含 `characters`中字符 (缺省是空白)的最長的字符串 | `trim(both 'x' from 'xTomxx')` | `Tom` | | ``upper(```string`) | `text` | 把字符串轉化為大寫 | `upper('tom')` | `TOM` | 還有額外的字符串操作函數可以用,它們在[Table 9-6](#calibre_link-2069)列出。 它們有些在內部用于實現[Table 9-5](#calibre_link-1044) 列出的SQL標準字符串函數。 **Table 9-6\. 其它字符串函數** | 函數 | 返回類型 | 描述 | 示例 | 結果 | | --- | --- | --- | --- | --- | | ``ascii(```string`) | `int` | 參數中第一個字符的ASCII編碼值。 對于UTF8返回字符的寬字節編碼值。 對于其它的多字節編碼,參數必須是一個ASCII字符。 | `ascii('x')` | `120` | | ``btrim(```string` `text` [, `characters` `text`]) | `text` | 從`string`開頭和結尾刪除只包含 `characters`中字符(缺省是空白)的最長字符串。 | `btrim('xyxtrimyyx', 'xy')` | `trim` | | ``chr(```int`) | `text` | 給定編碼的字符。對于UTF8這個參數作為寬字節代碼處理。 對于其它的多字節編碼,這個參數必須指定一個ASCII字符, 因為text數據類型無法存儲NULL數據字節,不能將NULL(0)作為字符參數。 | `chr(65)` | `A` | | ``concat(```str` `"any"` [, `str` `"any"` [, ...] ]) | `text` | 連接所有參數的文本表示。NULL 參數被忽略。 | `concat('abcde', 2, NULL, 22)` | `abcde222` | | ``concat_ws(```sep` `text`, `str` `"any"` [, `str` `"any"` [, ...] ]) | `text` | 連接所有參數,但是第一個參數是分隔符,用于將所有參數分隔。NULL 參數被忽略。 | `concat_ws(',', 'abcde', 2, NULL, 22)` | `abcde,2,22` | | ``convert(```string` `bytea`, `src_encoding` `name`, `dest_encoding` `name`) | `bytea` | 把原來編碼為`src_encoding`的字符串轉換為 `dest_encoding`編碼。 在這種編碼格式中`string`必須是有效的。 用`CREATE CONVERSION`定義轉換。 這也有些預定義的轉換。 參閱[Table 9-7](#calibre_link-2070)顯示可用的轉換。 | `convert('text_in_utf8', 'UTF8', 'LATIN1')` | `text_in_utf8` 用 Latin-1 編碼表示 (ISO 8859-1) | | ``convert_from(```string` `bytea`, `src_encoding` `name`) | `text` | 把原來編碼為`src_encoding`的字符串轉換為數據庫編碼格式。 這種編碼格式中,`string`必須是有效的。 | `convert_from('text_in_utf8', 'UTF8')` | `text_in_utf8` 用當前數據庫編碼表示 | | ``convert_to(```string` `text`, `dest_encoding` `name`) | `bytea` | 將字符串轉化為`dest_encoding`編碼格式。 | `convert_to('some text', 'UTF8')` | `some text` 用 UTF8 編碼表示 | | ``decode(```string` `text`, `format` `text`) | `bytea` | 把用`string`表示的文本里面的二進制數據解碼。 `format`選項和`encode`相同。 | `decode('MTIzAAE=', 'base64')` | `\x3132330001` | | ``encode(```data` `bytea`, `format` `text`) | `text` | 把二進制數據編碼為文本表示。支持的格式有:`base64`, `hex`, `escape`。`escape` 轉換零字節和高位設置字節為八進制序列(`\``_nnn_`) 和雙反斜杠。 | `encode(E'123\\000\\001', 'base64')` | `MTIzAAE=` | | ``format`(``formatstr` `text` [, `formatarg` `"any"` [, ...] ]) | `text` | 根據格式字符串格式參數。這個函數類似C函數`sprintf`。 參閱[Section 9.4.1](#calibre_link-1813)。 | `format('Hello %s, %1$s', 'World')` | `Hello World, World` | | ``initcap(```string`) | `text` | 把每個單詞的第一個字母轉為大寫,其它的保留小寫。 單詞是一系列字母數字組成的字符,用非字母數字分隔。 | `initcap('hi THOMAS')` | `Hi Thomas` | | ``left(```str` `text`, `n` `int`) | `text` | 返回字符串的前`_n_`個字符。當`_n_`是負數時, 返回除最后&#124;`_n_`&#124;個字符以外的所有字符。 | `left('abcde', 2)` | `ab` | | ``length(```string`) | `int` | `string`中字符的數目 | `length('jose')` | `4` | | ``length(```string` `bytea`, `encoding` `name` ) | `int` | 指定`encoding`編碼格式的`string`的字符數。 在這個編碼格式中,`string`必須是有效的。 | `length('jose', 'UTF8')` | `4` | | ``lpad(```string` `text`, `length` `int` [, `fill` `text`]) | `text` | 通過填充字符`fill`(缺省時為空白), 把`string`填充為`length`長度。 如果`string`已經比`length`長則將其尾部截斷。 | `lpad('hi', 5, 'xy')` | `xyxhi` | | ``ltrim(```string` `text` [, `characters` `text`]) | `text` | 從字符串`string`的開頭刪除只包含`characters` 中字符(缺省是一個空白)的最長的字符串。 | `ltrim('zzzytrim', 'xyz')` | `trim` | | ``md5(```string`) | `text` | 計算`string`的MD5散列,以十六進制返回結果。 | `md5('abc')` | `900150983cd24fb0 d6963f7d28e17f72` | | ``pg_client_encoding()`` | `name` | 當前客戶端編碼名稱 | `pg_client_encoding()` | `SQL_ASCII` | | ``quote_ident(```string` `text`) | `text` | 返回適用于SQL語句的標識符形式(使用適當的引號進行界定)。 只有在必要的時候才會添加引號(字符串包含非標識符字符或者會轉換大小寫的字符)。 嵌入的引號被恰當地寫了雙份。又見[Example 40-1](#calibre_link-1702)。 | `quote_ident('Foo bar')` | `"Foo bar"` | | ``quote_literal(```string` `text`) | `text` | 返回適用于在SQL語句里當作文本使用的形式(使用適當的引號進行界定)。 嵌入的引號和反斜杠被恰當地寫了雙份。請注意,當輸入是null時, `quote_literal`返回null;如果參數可能為null, 通常`quote_nullable`更適用。 又見[Example 40-1](#calibre_link-1702)。 | `quote_literal(E'O\'Reilly')` | `'O''Reilly'` | | ``quote_literal(```value` `anyelement`) | `text` | 將給定的值強制轉換為text,加上引號作為文本。嵌入的引號和反斜杠被恰當地寫了雙份。 | `quote_literal(42.5)` | `'42.5'` | | ``quote_nullable(```string` `text`) | `text` | 返回適用于在SQL語句里當作字符串使用的形式(使用適當的引號進行界定)。 或者,如果參數為空,返回`NULL`。嵌入的引號和反斜杠被恰當地寫了雙份。 又見[Example 40-1](#calibre_link-1702)。 | `quote_nullable(NULL)` | `NULL` | | ``quote_nullable(```value` `anyelement`) | `text` | 將給定的參數值轉化為text,加上引號作為文本;或者,如果參數為空, 返回`NULL`。嵌入的引號和反斜杠被恰當地寫了雙份。 | `quote_nullable(42.5)` | `'42.5'` | | ``regexp_matches(```string` `text`, `pattern` `text` [, `flags` `text`]) | `setof text[]` | 返回`string`中所有匹配POSIX正則表達式的子字符串。 參閱[Section 9.7.3](#calibre_link-1066)獲得更多信息。 | `regexp_matches('foobarbequebaz', '(bar)(beque)')` | `{bar,beque}` | | ``regexp_replace(```string` `text`, `pattern` `text`, `replacement` `text` [, `flags` `text`]) | `text` | 替換匹配 POSIX 正則表達式的子字符串。參見[Section 9.7.3](#calibre_link-1066) 以獲取更多模式匹配的信息。 | `regexp_replace('Thomas', '.[mN]a.', 'M')` | `ThM` | | ``regexp_split_to_array(```string` `text`, `pattern` `text` [, `flags` `text` ]) | `text[]` | 用POSIX正則表達式作為分隔符,分隔`string`。 參閱[Section 9.7.3](#calibre_link-1066)以獲取更多模式匹配的信息。 | `regexp_split_to_array('hello world', E'\\s+')` | `{hello,world}` | | ``regexp_split_to_table(```string` `text`, `pattern` `text` [, `flags` `text`]) | `setof text` | 用POSIX正則表達式作為分隔符,分隔`string`。 參閱[Section 9.7.3](#calibre_link-1066)以獲取更多模式匹配的信息。 | `regexp_split_to_table('hello world', E'\\s+')` | `hello` `world` (2 rows) | | ``repeat(```string` `text`, `number` `int`) | `text` | 將`string`重復`number`次 | `repeat('Pg', 4)` | `PgPgPgPg` | | ``replace(```string` `text`, `from` `text`, `to` `text`) | `text` | 把字符串`string`里出現地所有子字符串`from` 替換成子字符串`to` | `replace('abcdefabcdef', 'cd', 'XX')` | `abXXefabXXef` | | ``reverse(```str`) | `text` | 返回顛倒的字符串 | `reverse('abcde')` | `edcba` | | ``right(```str` `text`, `n` `int`) | `text` | 返回字符串中的后`_n_`個字符。當`_n_`是負值時, 返回除前&#124;`_n_`&#124;個字符以外的所有字符。 | `right('abcde', 2)` | `de` | | ``rpad(```string` `text`, `length` `int` [, `fill` `text`]) | `text` | 使用填充字符`fill`(缺省時為空白), 把`string`填充到`length`長度。 如果`string`已經比`length`長則將其從尾部截斷。 | `rpad('hi', 5, 'xy')` | `hixyx` | | ``rtrim(```string` `text` [, `characters` `text`]) | `text` | 從字符串`string`的結尾刪除只包含 `characters`中字符(缺省是個空白)的最長的字符串。 | `rtrim('trimxxxx', 'x')` | `trim` | | ``split_part(```string` `text`, `delimiter` `text`, `field` `int`) | `text` | 根據`delimiter`分隔`string` 返回生成的第 field 個子字符串(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`]) | `text` | 把`string`從其它編碼轉換為ASCII (僅支持`LATIN1`, `LATIN2`, `LATIN9`, `WIN1250`編碼)。 | `to_ascii('Karel')` | `Karel` | | ``to_hex(```number` `int` or `bigint`) | `text` | 把`number`轉換成十六進制表現形式 | `to_hex(2147483647)` | `7fffffff` | | ``translate(```string` `text`, `from` `text`, `to` `text`) | `text` | 把在`string`中包含的任何匹配`from` 中字符的字符轉化為對應的在`to`中的字符。 如果`from`比`to`長, 刪掉在`from`中出現的額外的字符。 | `translate('12345', '143', 'ax')` | `a2x5` | `concat`, `concat_ws`和`format` 函數是可變的,所以用`VARIADIC` 關鍵字標記傳遞的數值以連接或者格式化為一個數組是可能的。 見[Section 35.4.5](#calibre_link-914)。數組的元素對函數來說是單獨的普通參數。 如果可變數組的元素是NULL,那么`concat`和`concat_ws` 返回NULL,但是`format`把NULL作為零元素數組對待。 又見[Section 9.20](#calibre_link-1641)里面的聚集函數`string_agg`。 **Table 9-7\. 內置的轉換** | 轉換名 [[a]](#calibre_link-2071) | 源編碼 | 目的編碼 | | --- | --- | --- | | `ascii_to_mic` | `SQL_ASCII` | `MULE_INTERNAL` | | `ascii_to_utf8` | `SQL_ASCII` | `UTF8` | | `big5_to_euc_tw` | `BIG5` | `EUC_TW` | | `big5_to_mic` | `BIG5` | `MULE_INTERNAL` | | `big5_to_utf8` | `BIG5` | `UTF8` | | `euc_cn_to_mic` | `EUC_CN` | `MULE_INTERNAL` | | `euc_cn_to_utf8` | `EUC_CN` | `UTF8` | | `euc_jp_to_mic` | `EUC_JP` | `MULE_INTERNAL` | | `euc_jp_to_sjis` | `EUC_JP` | `SJIS` | | `euc_jp_to_utf8` | `EUC_JP` | `UTF8` | | `euc_kr_to_mic` | `EUC_KR` | `MULE_INTERNAL` | | `euc_kr_to_utf8` | `EUC_KR` | `UTF8` | | `euc_tw_to_big5` | `EUC_TW` | `BIG5` | | `euc_tw_to_mic` | `EUC_TW` | `MULE_INTERNAL` | | `euc_tw_to_utf8` | `EUC_TW` | `UTF8` | | `gb18030_to_utf8` | `GB18030` | `UTF8` | | `gbk_to_utf8` | `GBK` | `UTF8` | | `iso_8859_10_to_utf8` | `LATIN6` | `UTF8` | | `iso_8859_13_to_utf8` | `LATIN7` | `UTF8` | | `iso_8859_14_to_utf8` | `LATIN8` | `UTF8` | | `iso_8859_15_to_utf8` | `LATIN9` | `UTF8` | | `iso_8859_16_to_utf8` | `LATIN10` | `UTF8` | | `iso_8859_1_to_mic` | `LATIN1` | `MULE_INTERNAL` | | `iso_8859_1_to_utf8` | `LATIN1` | `UTF8` | | `iso_8859_2_to_mic` | `LATIN2` | `MULE_INTERNAL` | | `iso_8859_2_to_utf8` | `LATIN2` | `UTF8` | | `iso_8859_2_to_windows_1250` | `LATIN2` | `WIN1250` | | `iso_8859_3_to_mic` | `LATIN3` | `MULE_INTERNAL` | | `iso_8859_3_to_utf8` | `LATIN3` | `UTF8` | | `iso_8859_4_to_mic` | `LATIN4` | `MULE_INTERNAL` | | `iso_8859_4_to_utf8` | `LATIN4` | `UTF8` | | `iso_8859_5_to_koi8_r` | `ISO_8859_5` | `KOI8R` | | `iso_8859_5_to_mic` | `ISO_8859_5` | `MULE_INTERNAL` | | `iso_8859_5_to_utf8` | `ISO_8859_5` | `UTF8` | | `iso_8859_5_to_windows_1251` | `ISO_8859_5` | `WIN1251` | | `iso_8859_5_to_windows_866` | `ISO_8859_5` | `WIN866` | | `iso_8859_6_to_utf8` | `ISO_8859_6` | `UTF8` | | `iso_8859_7_to_utf8` | `ISO_8859_7` | `UTF8` | | `iso_8859_8_to_utf8` | `ISO_8859_8` | `UTF8` | | `iso_8859_9_to_utf8` | `LATIN5` | `UTF8` | | `johab_to_utf8` | `JOHAB` | `UTF8` | | `koi8_r_to_iso_8859_5` | `KOI8R` | `ISO_8859_5` | | `koi8_r_to_mic` | `KOI8R` | `MULE_INTERNAL` | | `koi8_r_to_utf8` | `KOI8R` | `UTF8` | | `koi8_r_to_windows_1251` | `KOI8R` | `WIN1251` | | `koi8_r_to_windows_866` | `KOI8R` | `WIN866` | | `koi8_u_to_utf8` | `KOI8U` | `UTF8` | | `mic_to_ascii` | `MULE_INTERNAL` | `SQL_ASCII` | | `mic_to_big5` | `MULE_INTERNAL` | `BIG5` | | `mic_to_euc_cn` | `MULE_INTERNAL` | `EUC_CN` | | `mic_to_euc_jp` | `MULE_INTERNAL` | `EUC_JP` | | `mic_to_euc_kr` | `MULE_INTERNAL` | `EUC_KR` | | `mic_to_euc_tw` | `MULE_INTERNAL` | `EUC_TW` | | `mic_to_iso_8859_1` | `MULE_INTERNAL` | `LATIN1` | | `mic_to_iso_8859_2` | `MULE_INTERNAL` | `LATIN2` | | `mic_to_iso_8859_3` | `MULE_INTERNAL` | `LATIN3` | | `mic_to_iso_8859_4` | `MULE_INTERNAL` | `LATIN4` | | `mic_to_iso_8859_5` | `MULE_INTERNAL` | `ISO_8859_5` | | `mic_to_koi8_r` | `MULE_INTERNAL` | `KOI8R` | | `mic_to_sjis` | `MULE_INTERNAL` | `SJIS` | | `mic_to_windows_1250` | `MULE_INTERNAL` | `WIN1250` | | `mic_to_windows_1251` | `MULE_INTERNAL` | `WIN1251` | | `mic_to_windows_866` | `MULE_INTERNAL` | `WIN866` | | `sjis_to_euc_jp` | `SJIS` | `EUC_JP` | | `sjis_to_mic` | `SJIS` | `MULE_INTERNAL` | | `sjis_to_utf8` | `SJIS` | `UTF8` | | `tcvn_to_utf8` | `WIN1258` | `UTF8` | | `uhc_to_utf8` | `UHC` | `UTF8` | | `utf8_to_ascii` | `UTF8` | `SQL_ASCII` | | `utf8_to_big5` | `UTF8` | `BIG5` | | `utf8_to_euc_cn` | `UTF8` | `EUC_CN` | | `utf8_to_euc_jp` | `UTF8` | `EUC_JP` | | `utf8_to_euc_kr` | `UTF8` | `EUC_KR` | | `utf8_to_euc_tw` | `UTF8` | `EUC_TW` | | `utf8_to_gb18030` | `UTF8` | `GB18030` | | `utf8_to_gbk` | `UTF8` | `GBK` | | `utf8_to_iso_8859_1` | `UTF8` | `LATIN1` | | `utf8_to_iso_8859_10` | `UTF8` | `LATIN6` | | `utf8_to_iso_8859_13` | `UTF8` | `LATIN7` | | `utf8_to_iso_8859_14` | `UTF8` | `LATIN8` | | `utf8_to_iso_8859_15` | `UTF8` | `LATIN9` | | `utf8_to_iso_8859_16` | `UTF8` | `LATIN10` | | `utf8_to_iso_8859_2` | `UTF8` | `LATIN2` | | `utf8_to_iso_8859_3` | `UTF8` | `LATIN3` | | `utf8_to_iso_8859_4` | `UTF8` | `LATIN4` | | `utf8_to_iso_8859_5` | `UTF8` | `ISO_8859_5` | | `utf8_to_iso_8859_6` | `UTF8` | `ISO_8859_6` | | `utf8_to_iso_8859_7` | `UTF8` | `ISO_8859_7` | | `utf8_to_iso_8859_8` | `UTF8` | `ISO_8859_8` | | `utf8_to_iso_8859_9` | `UTF8` | `LATIN5` | | `utf8_to_johab` | `UTF8` | `JOHAB` | | `utf8_to_koi8_r` | `UTF8` | `KOI8R` | | `utf8_to_koi8_u` | `UTF8` | `KOI8U` | | `utf8_to_sjis` | `UTF8` | `SJIS` | | `utf8_to_tcvn` | `UTF8` | `WIN1258` | | `utf8_to_uhc` | `UTF8` | `UHC` | | `utf8_to_windows_1250` | `UTF8` | `WIN1250` | | `utf8_to_windows_1251` | `UTF8` | `WIN1251` | | `utf8_to_windows_1252` | `UTF8` | `WIN1252` | | `utf8_to_windows_1253` | `UTF8` | `WIN1253` | | `utf8_to_windows_1254` | `UTF8` | `WIN1254` | | `utf8_to_windows_1255` | `UTF8` | `WIN1255` | | `utf8_to_windows_1256` | `UTF8` | `WIN1256` | | `utf8_to_windows_1257` | `UTF8` | `WIN1257` | | `utf8_to_windows_866` | `UTF8` | `WIN866` | | `utf8_to_windows_874` | `UTF8` | `WIN874` | | `windows_1250_to_iso_8859_2` | `WIN1250` | `LATIN2` | | `windows_1250_to_mic` | `WIN1250` | `MULE_INTERNAL` | | `windows_1250_to_utf8` | `WIN1250` | `UTF8` | | `windows_1251_to_iso_8859_5` | `WIN1251` | `ISO_8859_5` | | `windows_1251_to_koi8_r` | `WIN1251` | `KOI8R` | | `windows_1251_to_mic` | `WIN1251` | `MULE_INTERNAL` | | `windows_1251_to_utf8` | `WIN1251` | `UTF8` | | `windows_1251_to_windows_866` | `WIN1251` | `WIN866` | | `windows_1252_to_utf8` | `WIN1252` | `UTF8` | | `windows_1256_to_utf8` | `WIN1256` | `UTF8` | | `windows_866_to_iso_8859_5` | `WIN866` | `ISO_8859_5` | | `windows_866_to_koi8_r` | `WIN866` | `KOI8R` | | `windows_866_to_mic` | `WIN866` | `MULE_INTERNAL` | | `windows_866_to_utf8` | `WIN866` | `UTF8` | | `windows_866_to_windows_1251` | `WIN866` | `WIN` | | `windows_874_to_utf8` | `WIN874` | `UTF8` | | `euc_jis_2004_to_utf8` | `EUC_JIS_2004` | `UTF8` | | `utf8_to_euc_jis_2004` | `UTF8` | `EUC_JIS_2004` | | `shift_jis_2004_to_utf8` | `SHIFT_JIS_2004` | `UTF8` | | `utf8_to_shift_jis_2004` | `UTF8` | `SHIFT_JIS_2004` | | `euc_jis_2004_to_shift_jis_2004` | `EUC_JIS_2004` | `SHIFT_JIS_2004` | | `shift_jis_2004_to_euc_jis_2004` | `SHIFT_JIS_2004` | `EUC_JIS_2004` | | Notes: a. 轉換名遵循一個標準的命名模式:將源編碼中的所有非字母數字字符用下劃線替換, 后面跟著`_to_`,然后后面再跟著經過相似處理的目標編碼的名字。 因此這些名字可能和客戶的編碼名字不同。 | ## 9.4.1\. `格式化` 函數`format`生成根據格式字符串格式化了的輸出,風格類似于C函數`sprintf`。 ``` format(formatstr text [, formatarg "any" [, ...] ]) ``` `_formatstr_`是指定結果如何格式化的格式字符串。格式字符串中的文本直接拷貝到結果中, 除非已經使用了_格式說明符_。格式說明符在字符串中作為占位符使用, 定義后續函數參數應該格式化并且插入到結果中。每個`_formatarg_` 參數根據這種數據類型的通常輸出規則轉化為文本,然后根據格式說明符格式化并且插入到結果中。 格式說明符由`%`字符引進,格式為 ``` %[_position_][_flags_][_width_]_type_ ``` 組件的字段有: `_position_` (optional) `_n_`$格式的字符串,這里的`_n_` 是要打印的參數的索引。索引為1表示在`_formatstr_` 之后的第一個參數。如果省略了`_formatstr_`, 默認使用序列中的下一個參數。 `_flags_` (optional) 附加選項,控制如何格式化格式說明符的輸出。當前只支持負號(`-`), 負號導致格式說明符的輸出是左對齊的。這是沒有影響的,除非指定了 `_width_`字段。 `_width_` (optional) 聲明字符數的_minimum_值用來顯示格式說明符的輸出。需要補充寬度時, 空白添加到輸出的左側或右側(取決于`-`標志)。一個比較小的寬度不會導致輸出的截斷, 只是簡單的忽略了。寬度可以用下列方法指定:一個正整數;一個星號(`*`) 表示使用下一個函數參數作為寬度;或一個格式為`*``_n_`$ 的字符串表示使用第`_n_`個函數參數作為寬度。 如果寬度來自函數參數,那么這個參數在作為格式說明符的數值之前消耗掉。 如果寬度參數是負的,那么結果是左對齊的(就像聲明了`-`標志一樣), 并且字段長度為`abs`(`_width_`)。 `_type_` (required) 格式轉換的類型用來產生格式說明符的輸出。支持下列的類型: * `s`格式參數值為簡單的字符串。空值作為空字符串對待。 * `I`將參數值作為SQL標識符對待,如果需要,雙寫它。值為空是錯誤的。 * `L`引用參數值作為SQL文字。空值用字符串`NULL`顯示,沒有引用。 除了上述的格式說明符,特殊的序列`%%`可以用作輸出`%`字符。 這里有一些基本的格式轉換的例子: ``` SELECT format('Hello %s', 'World'); _Result:_ <samp class="literal">Hello World</samp> SELECT format('Testing %s, %s, %s, %%', 'one', 'two', 'three'); _Result:_ <samp class="literal">Testing one, two, three, %</samp> SELECT format('INSERT INTO %I VALUES(%L)', 'Foo bar', E'O\'Reilly'); _Result:_ <samp class="literal">INSERT INTO "Foo bar" VALUES('O''Reilly')</samp> SELECT format('INSERT INTO %I VALUES(%L)', 'locations', E'C:\\Program Files'); _Result:_ <samp class="literal">INSERT INTO locations VALUES(E'C:\\Program Files')</samp> ``` 這里是使用`_width_`字段和`-`標志的例子: ``` SELECT format('|%10s|', 'foo'); _Result:_ <samp class="literal">| foo|</samp> SELECT format('|%-10s|', 'foo'); _Result:_ <samp class="literal">|foo |</samp> SELECT format('|%*s|', 10, 'foo'); _Result:_ <samp class="literal">| foo|</samp> SELECT format('|%*s|', -10, 'foo'); _Result:_ <samp class="literal">|foo |</samp> SELECT format('|%-*s|', 10, 'foo'); _Result:_ <samp class="literal">|foo |</samp> SELECT format('|%-*s|', -10, 'foo'); _Result:_ <samp class="literal">|foo |</samp> ``` 下面是使用`_position_`字段的例子: ``` SELECT format('Testing %3$s, %2$s, %1$s', 'one', 'two', 'three'); _Result:_ <samp class="literal">Testing three, two, one</samp> SELECT format('|%*2$s|', 'foo', 10, 'bar'); _Result:_ <samp class="literal">| bar|</samp> SELECT format('|%1$*2$s|', 'foo', 10, 'bar'); _Result:_ <samp class="literal">| foo|</samp> ``` 和C函數`sprintf`不同,PostgreSQL的`format` 函數允許帶有或不帶有`_position_`字段的格式說明符在相同的格式字符串中混合使用。 沒有`_position_`字段的格式說明符總是使用最后消耗參數的下一個參數。 另外,`format`函數不要求在格式字符串中使用所有函數參數。例如: ``` SELECT format('Testing %3$s, %2$s, %s', 'one', 'two', 'three'); _Result:_ <samp class="literal">Testing three, two, three</samp> ``` `%I`和`%L`格式說明符對于安全構造動態SQL語句尤其有用。 參閱[Example 40-1](#calibre_link-1702)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看