Provide Datetime formatting while BCP export - bcp

BCP export outputs dates in yyyy-mm-dd format, I need them to be in yyyy.mm.dd format. The export file is consumed by another application which is rigid.
I am going to do bcp export for large number of tables and it would be great if I can apply this formatting to all date columns in a result set without explicitly specifying the date columns.

Use CONVERT to get the desired format and then REPLACE to have '.' instead of '-':
BCP "SELECT REPLACE(CONVERT(char(10), GetDate(),126),'-','.')" QUERYOUT "d:\out.txt" -c -T
This is the content of the output file:
2018.03.02

Related

Export out CSV Files from SQL Based of Dates

I am trying to figure out how to generate multiple csv exports from a table in SQL based off the date field in this table. The date is different for many records. I would like to export records containing the same date to a csv. Every time a different date is found, a csv will be exported containing the data for the selected columns and date. How can I go about creating a script to perform this type of action. Is there a way to have the script go through the date column automatically and export out the data with the 4 fields selected and generated a csv for those docs that share the same date, etc.
Example:
Select Box, Highprice, Lowprice, Date
where date="2016-01-31"
As others suggest this is not a good method. Also there are few issues related to this.
By default, sql server is not granting permission to execute xp_cmdshell
The files will be generated in the Server and should have proper accessing privileges for that.
I gave you the way to do it for a static date and you may need to write a cursor to loop all days that are needed.
select #sql = 'bcp YourDB.dbo.YourTbl where date=''2016-01-31'' out
c:\temp\CSV2016-01-31.csv -c -t, -T -S'+ ##servername
exec master..xp_cmdshell #sql

Export to Excel from SQL server through BCP

I tried to export the result of a query to an excel sheet with the following BCP command and I was successful.
But the issue is, one of the columns have data with delimiter. Because of that the structure of the data is affected when I open the file in MS Excel. The data is not coming in the desired cell.
Is there anyway to escape the delimiter in the data of the column.
set #cmd='bcp "select ''colName1'',''colName2'',''colName3'',''colName4'',''colName5'',''colName6'',''colName7'' union all SELECT colValue1,colValue2,CONVERT(varchar,[colValue3]),[colValue4],convert(varchar,[colValue5]),[colValue6],[colValue7] from [DBName].[dbo].[ViewName] where  [colValue5]='''+ CONVERT(VARCHAR(8), GETDATE()-1, 112)+'''" queryout "HYPERLINK "D:\Report_'+CONVERT(VARCHAR, GETDATE(), 112)+'.xls"D:\Reports\Detailed_Comment_Report_'+CONVERT(VARCHAR(8), GETDATE(), 112)+'.xls" -c -T -t -S ABCD' 

How to change the date format when the file is spooled in oracle

Currently i'm generating a export where the date format is actually dd/mm/yyyy but when i spool it and see it is coming as dd/mmm/yyyy in a readable format
For example if the date in my real file is 06/10/2014 in the spool it automatically is coming 06-Oct-2014. But i don't want this format. How to retain the original one?
You can convert your date column to char representation before exporting
to_char(date_column, 'dd/mm/yyyy')
There is no such thing as an original date format. Oracle stores dates without any format. When you're spooling your data, you either have to specifically indicate a format or Oracle uses a system default format.
So, if you consistantly want the format to be dd/mm/yyyy then you could change the default format for just a session with
alter session set nls_date_format = 'dd/mm/yyyy'

DB2 SQL query returns some type of converted results when exporting to file

have shell script which queries a DB2 db and exports the output to a file. When I sun the SQL statement without exporting, I get the following:
su - myid -c 'db2 connect to mydb;db2 -v "select COL1"; db2 connect reset;'
Sample Output
COL 1
x'20A0E2450080000'
x'50D24520E100GDS00'
x'10H0EFJ10080000'
x'50A0GH0080000'
x'80RHE1008B0000'
x'70A50E1F4008000'
x'10F329EF09BB0'
But when I export my results using the exact same query, I get the following:
su - myid -c 'db2 connect to mydb;db2 -v "EXPORT TO '/tmp/query_results.out' OF DEL MODIFIED BY COLDEL: select COL1 from MYTABLE"; db2 connect reset;'
Sample Output
hôª"
"xàÓ °á
"èÅ °á
hôª"
"é# °á
hôª"
"é« °á
hôª"
"éÅ °á
hôª"
"""ÒYá  á
hôª"
"#sYá  á
hôª"
I'm assuming this is due to the single quote characters. Due to the fact that they are both preceded by another character, I have not been able to add '\' in front of them. I've also attempted to run the substr function within the query, but I still get the same result, only shorter. I'm sure there must be something I am overlooking, so after a several days of trying on my own (and failing), I'm turning to you guys. Any help would be greatly appreciated.
*Edit: Just wanted to add that my actual select statement includes more than one column which are displayed correctly. So out of several columns, only one is displaying bad data.
"I'm assuming this is due to the single quote characters" -- No. This particular column contains binary data, either BLOB or VARCHAR FOR BIT DATA. If it is BLOB, specify LOBS TO in the EXPORT command, this way BLOBs will be written to binary files. If it is VARCHAR FOR BIT DATA, you can either convert it to BLOB on export (export to ... lobs to ... select blob(your_column)...) or export it as hex(your_column), depending on what you're planning to do with the export later.
Another alternative for VARCHAR FOR BIT DATA would be to export your table using the IXF format instead of DEL, which will preserve binary strings.

How to export text data from a SQL Server table?

I am trying to use the MS SQL Server 2005 Import/Export tool to export a table so I can import it into another database for archival. One of the columns is text so if I export as comma-delimited, when I try to import it into the archive table, it doesn't work correctly for rows with commas in that field. What options should I choose to ensure my import will work correctly?
Over a year later, I now have an ideal solution to my data export needs, thanks to https://stackoverflow.com/questions/20363/
bcp "SELECT * FROM CustomerTable" queryout "c:\temp\CustomerTable.bcp" -N -S SOURCESERVERNAME -T
bcp TargetDatabaseTable in "c:\temp\CustomerTable.bcp" -N -S TARGETSERVERNAME -T -E
-N use native types
-T use the trusted connection
-S ServerName
-E Keep identity values specified in the data file
Very quick and easy to embed within code.
I never use the comma delimter unless the client requires it. Try using | as a delimter. YOu can also use the text qualifier if need be.
Use quotes as text qualifier
Text qualifier:
Type the text qualifier to use. For example, you can specify that each text column be surrounded with quotation marks.