sql - Columns to rows and export (copy) to a text file -
i have following table
create table usr_addr_part_15 ( company character varying(70), add1 character varying(50), add2 character varying(50), add3 character varying(50), phoneoff character varying(30) )
with sample data follows
insert usr_addr_part_15 values ( 'a compnay' ,'35/908a4, kings arcade, north atlanta road' ,'fl' ,'ws-600 025' ,'011-200455/255477' ) ,( 'b company' ,'35/465r, queens arcade, west atlanta road' ,'fl' ,'wt-601 085' ,'0225-2455215/21470277' )
so need copy
contents in usr_addr_part_15
text file below given format :
company : compnay add1 : 35/908a4,kings arcade,north atlanta road add2 : fl add3 : ws-600 025 phone(off.) : 011-200455/255477 --- end of company 1 --- company : b company add1 : 35/465r,queens arcade,west atlanta road add2 : fl add3 : wt-601 085 phone(off.) : 0225-2455215/21470277 --- end of company 2 ---
on windows xp, pgadmin iii.
if understood correctly can :
select unnest(format('{company : %s,add1 : %s,add2 : %s,add3 : %s,phone(off.) : %s,--- end of company %s ---}', company, replace(add1, ',', ';'), replace(add2, ',', ';'), replace(add3, ',', ';'),phoneoff, row_number() on ())::text []) usr_addr_part_15
and copy querying
copy( select unnest(format('{company : %s,add1 : %s,add2 : %s,add3 : %s,phone(off.) : %s,--- end of company %s ---}', company, replace(add1, ',', ';'), replace(add2, ',', ';'), replace(add3, ',', ';'),phoneoff, row_number() on ())::text []) usr_addr_part_15 ) 'd:\test.txt' -- file name want save data
Comments
Post a Comment