I use Oracle and I would like to know how to keep leading/prefix zeros when export to Excel/csv by below SQL query statement?
Supposed B.h
(string type) maybe have the values of 00068573 and 098235
SELECT A.e,
A.f,
A.g,
B.h
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;
and I export data in Python, partial code as below:
for row_data in cursor:
output.writerow(row_data)
Thank you so much for any advice.
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
COLUMN CODE2 FORMAT 09999999
SET COLSEP ';'
spool test.csv
SELECT A.e,
A.f,
A.g,
B.h as code2
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;
spool off
exit;
or use to_char(B.h, '09999999')
set heading off
set termout OFF
SET FEEDBACK OFF
SET TAB OFF
set pause off
set verify off
SET UNDERLINE OFF
set trimspool on
set timing off
set echo off
set linesize 1000
set pagesize 0
SET COLSEP ';'
spool test.csv
SELECT A.e,
A.f,
A.g,
to_char(B.h, '09999999')
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;
spool off
exit;
if the field is character then you need to use the lpad function.
SELECT A.e,
A.f,
A.g,
LPAD(B.h, 9, '0')
FROM A
JOIN C ON C.e=A.e
JOIN B ON C.j=B.j;