PostgreSQL 文字型のYYYY/MM/DD を YYYYMMDD にする
文字型のカラムに ymd に '2012/12/12' みたいな日付文字列が格納されている。
'/' を消して '20121212' みたいに変換したくなった。
当初 substring(ymd, 1,4) || substring(ymd, 6,2) || substring(ymd,9,2) みたいなことを考えたんだけど、単に replace で置換でいいじゃんね。
test=> create table hage_table ( ymd varchar(10) ); CREATE TABLE test=> insert into hage_table (ymd) values('20121211'); INSERT 0 1 test=> insert into hage_table (ymd) values('2012/12/12'); INSERT 0 1 test=> select * from hage_table; ymd ------------ 20121211 2012/12/12 (2 rows) test=> update hage_table set ymd = replace(ymd, '/', ''); UPDATE 2 test=> select * from hage_table; ymd ---------- 20121211 20121212 (2 rows)