Exporting SQL data to Excel without losing the 0 in the phone number

 ̄綄美尐妖づ 提交于 2019-12-13 06:08:22

问题


I currently have a simple database which stores a customers basic table. However when ever I try to export my sql query to Excel the phone number field always appears without a 0.

My SQL query results:

ID | Name |Address |DOB         | Gender |Phone_Number   |
---------------------------------------------------------
01 | Max  |Abc Road| 2000-12-19 | Male   |07777 893 8902 |
02 | Sam  |TBH Road| null       | Male   |077778938902   |
03 | Doe  |Dr  Road| 1999-11-13 | Male   |077778 938902  |

What I get in Excel:

ID | Name |Address |DOB         | Gender |Phone_Number   |
---------------------------------------------------------
01 | Max  |Abc Road| 2000-12-19 | Male   |07777 893 8902 |
02 | Sam  |TBH Road|            | Male   |77778938902    |
03 | Doe  |Dr  Road| 1999-11-13 | Male   |077778 938902  |

However what I want to get this without having to format the cells in excel:

    ID | Name |Address |DOB         | Gender |Phone_Number   |
    ---------------------------------------------------------
    01 | Max  |Abc Road| 2000-12-19 | Male   |07777 893 8902 |
    02 | Sam  |TBH Road|            | Male   |077778938902   |
    03 | Doe  |Dr  Road| 1999-11-13 | Male   |077778 938902  |

My SQL query at the moment is as following:

SELECT 
ID,
isnull(Gender,'') Gender,
isnull(Name,'') Name,
isnull(Address,'') Address,
isnull(DOB,'') DOB,
isnull(Gender,'') Gender,
isnull(Phone_Numer,'') Phone_Number
FROM CUSTOMER;

回答1:


Try inserting a single quote ( ' ) as first character when you write that to Excel. Should format the cell to text.




回答2:


You can add an apostrophe, to tell excel to treat it as text...

    SELECT 
    ID,
    isnull(Gender,'') Gender,
    isnull(Name,'') Name,
    isnull(Address,'') Address,
    isnull(DOB,'') DOB,
    isnull(Gender,'') Gender,
    '''' + isnull(Phone_Numer,'') Phone_Number
    FROM CUSTOMER;


来源:https://stackoverflow.com/questions/32393158/exporting-sql-data-to-excel-without-losing-the-0-in-the-phone-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!