DB

Contents
現象を再現するためのテーブル作成
この全角ダッシュが混入するのは主に住所や固有名詞だと思いますが、そのサンプルテーブルを作るのは面倒なので、当該の# create table example_utf8_e28094 as select convert_from('\xe28094', 'utf8');
クエリが
場面 1 : Windows コマンドプロンプト + psql でデータを表示しようとした
日本語版# set lc_messages to 'ja_JP'; SET # select * from example_utf8_e28094; ERROR: 符号化方式"UTF8"における0xe2 0x80 0x94バイトシーケンス を持つ文字は"SJIS"符号化方式では等しくありません # set lc_messages to 'C'; SET # select * from example_utf8_e28094; ERROR: character with byte sequence 0xe2 0x80 0x94 in encoding "UTF8" has no equivalent in encoding "SJIS"

エラーが出たら原因の特定が第一。今回のケースでは「SJIS
代わりにクエリでバイトシーケンスから
# select to_hex( ascii( convert_from('\xe28094', 'utf8') ) ); +--------+ | to_hex | +--------+ | 2014 | +--------+ (1 row)

別の方法。psql
# \encoding utf8 # select * from example_utf8_e28094 \g r:/test/check.txt


psql
今回は、どんな文字コードでも使える
# select replace(text, convert_from('\xe28094', 'utf8'), '-') from example_utf8_e28094; +---------+ | replace | +---------+ | - | +---------+ (1 行)

上の画像では、念のためクライアントエンコーディングを
今回に限らず
場面 2 : COPY コマンドで Shift JIS ファイルに出力しようとした
今度は、UTF-8# \encoding utf8 # select * from example_utf8_e28094; +------+ | text | +------+ | — | +------+ (1 row) # copy example_utf8_e28094 to 'r:/test/output.txt' (encoding 'sjis'); ERROR: 符号化方式"UTF8"における0xe2 0x80 0x94バイトシーケンス を持つ文字は"SJIS"符号化方式では等しくありません
あえて
# copy ( select replace(text, convert_from('\xe28094', 'utf8'), '-') from example_utf8_e28094 ) to 'r:/test/output.txt' (encoding 'sjis'); COPY 1

もし「全角のままでくれ」と言われたら ↓ これとか。正確にはダッシュ記号じゃないけど(Windows7
# copy ( select replace(text, convert_from('\xe28094', 'utf8'),convert_from('\x817c', 'sjis') ) from example_utf8_e28094 ) to 'r:/test/output2.txt' (encoding 'sjis'); COPY 1
このように