【PostgreSQL】半角を全角に置換する

スポンサーリンク

説明

半角を全角に置換します。

引数

引数1(character varying):置換する文字列

返り値

置換した文字列

コード

「PgSQL」文字から2つ左のアイコンをクリックし、Ctrl+Cでコピーすると使いやすいです。↓↓

※コードの白黒画面を戻す場合は「<>」ボタンを押してください。

CREATE OR REPLACE FUNCTION convert_half_to_full(character varying)
  RETURNS character varying AS
$BODY$
declare

	a_char		alias for $1;	--変換対象の文字列
	a_convert_char	character varying;	--

begin

	a_convert_char = translate(
		a_char,
		'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォカケッャュョワ !"#$%&()ー-=^~|@`「[{;+:*」]}、,<。.>・/?_\',
		'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホマミムメモヤユヨラリルレロワヲンァィゥェォヵヶッャュョヮ !”#$%&()ー-=^~|@‘「[{;+:*」]}、,<。.>・/?_¥'
	);

	a_convert_char = replace(a_convert_char,'ヴ','ヴ' );
	a_convert_char = replace(a_convert_char,'ガ','ガ' );
	a_convert_char = replace(a_convert_char,'ギ','ギ' );
	a_convert_char = replace(a_convert_char,'グ','グ' );
	a_convert_char = replace(a_convert_char,'ゲ','ゲ' );
	a_convert_char = replace(a_convert_char,'ゴ','ゴ' );
	a_convert_char = replace(a_convert_char,'ザ','ザ' );
	a_convert_char = replace(a_convert_char,'ジ','ジ' );
	a_convert_char = replace(a_convert_char,'ズ','ズ' );
	a_convert_char = replace(a_convert_char,'ゼ','ゼ' );
	a_convert_char = replace(a_convert_char,'ゾ','ゾ' );
	a_convert_char = replace(a_convert_char,'ダ','ダ' );
	a_convert_char = replace(a_convert_char,'ヂ','ヂ' );
	a_convert_char = replace(a_convert_char,'ヅ','ヅ' );
	a_convert_char = replace(a_convert_char,'デ','デ' );
	a_convert_char = replace(a_convert_char,'ド','ド' );
	a_convert_char = replace(a_convert_char,'バ','バ' );
	a_convert_char = replace(a_convert_char,'ビ','ビ' );
	a_convert_char = replace(a_convert_char,'ブ','ブ' );
	a_convert_char = replace(a_convert_char,'ベ','ベ' );
	a_convert_char = replace(a_convert_char,'ボ','ボ' );
	a_convert_char = replace(a_convert_char,'パ','パ' );
	a_convert_char = replace(a_convert_char,'ピ','ピ' );
	a_convert_char = replace(a_convert_char,'プ','プ' );
	a_convert_char = replace(a_convert_char,'ペ','ペ' );
	a_convert_char = replace(a_convert_char,'ポ','ポ' );

	return a_convert_char;

end;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION convert_half_to_full(character varying)
  OWNER TO postgres;

実行例

select * From convert_half_to_full('テスト');

⇒テスト