Search code examples
mysqlinto-outfile

Retaining Decimal 0 during mysql outfile


So I have a table structure like so,

CREATE TABLE DATA_0 (ID BIGINT(15), I_R_A int(10) DEFAULT NULL, I_NO varchar(200) DEFAULT NULL, V_x double(10,1) DEFAULT NULL);

With sample data like so,

ID,I_R_A,I_NO,V_x
1,10000,9895987650, 10000.0
2,60,2424, 60.0
3,70,34, 70.0
4,80,34245, 80.0

When executing following outfile query-

SELECT ID,I_R_A,I_NO,V_x INTO OUTFILE "/home/user/out.txt" FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' FROM DATA_0;

My output file looks like this-

ID,I_R_A,I_NO,V_x
1,10000,9895987650, 10000
2,60,2424, 60
3,70,34, 70
4,80,34245, 80

As you can see the decimal 0 has been removed from the column V_x. How can I retain this 0?


Solution

  • use FORMAT function

    SELECT ID,I_R_A,I_NO, REPLACE(FORMAT(V_x, 1), ',', '')
    INTO OUTFILE "/home/user/out.txt" 
    FIELDS TERMINATED BY ',' LINES TERMINATED BY '\r\n' 
    FROM DATA_0;