简体   繁体   中英

SQL Server 2005 -> Excel export doesn't keep data types?

Trying (and largely succeeding) to export the results of a query from SQL Server to Excel, like so:

insert into OPENROWSET('Microsoft.Jet.OLEDB.4.0',
    'Excel 8.0;Database=c:\exported excel files\exported_data.xls;',
    'SELECT * FROM [Query$]') SELECT dbo.blabbityblah FROM dbo.the_table

It works! Sort of. It does export the data to the excel file, but it puts it all in there as text, even though some of the columns are datetime and most of them are numbers. None of them are being convert()-ed in the query itself. I've tried preformatting the cells in the actual Excel file before running the query, but it ignores the existing formating and spits it all out as text again.

There's got to be a way to do this, right?

excel dont have data type, its text based and preformat not work becus it replace existing file. if u want datatype try MS Access.

Look into using a schema.ini file to define the datatypes in a csv or txt. when you open either in excel you may achieve what you want

[sample_out.csv]
Format=CSVDelimited
DecimalSymbol=.
Col1=DATE datetime
Col2=FName Text

Another approach you may want to look at depending on your needs is to use the import and export wizard. You can customize a query for the data and specify the data type in the wizard. If you are using a SKU other than Express you can the run it right away or save the SSIS package is generated for further manipulation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM