BCP date format issue while exporting data from SQL server to csv file

Alexander S 1 Reputation point
2021-07-25T16:01:38.723+00:00

I'm using BCP command in SQL server to generate data's in csv file.
issue: SQL output value is 25/07/2021 but in csv same column value showing as 25-07-2021. Column data type is already defined as Nvarchar(30) in table.

I want to show same sql output format in csv. kindly help on this.

Microsoft 365 and Office Excel For business Windows
SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Alexander S 1 Reputation point
    2021-07-26T02:45:31.19+00:00

    Hi Guys,

    Thanks for your replies. Only issue is Doc_date format.

    Find below details

    SQL Query:
    Declare @filename Nvarchar(100)
    Declare @commandcommand varchar(8000)
    select @filename = 'GL_Report.csv'

    SELECT @commandcommand ='bcp "select COMP_CODE,convert(nvarchar(50),DR_CR_CODE),CURRENCY,COST_CENTER,COST_DESC,convert(nvarchar(50),Doc_Date),PAY_PERIOD,SAP_ACC_CODE,GL_ACC_NAME,convert(nvarchar(50),DR_CR_AMOUNT),extra_1,extra_2 from GL_REPORT_TAB_DCUBE_auto(Nolock) " queryout E:\GL_Report\GL_File\Processed\'+@filename+' -c -t, -T -S '+@@servername

    exec master..xp_cmdshell @commandcommand

    SQL Output:

    117750-sql-output.png

    CSV file output:

    ![117708-csv-output.png]2

    0 comments No comments

  2. Seeya Xi-MSFT 16,586 Reputation points
    2021-07-26T03:31:06.677+00:00

    Hi @Alexander S ,

    CSV takes the time format of windows by default. Modify the date and time format in the regional settings in the control panel, make sure the format is yyyy-mm-dd.
    If the above method does not work, you can export the time format to text format with adding Double quotes.

    Best regards,
    Seeya


    If the response is helpful, please click "Accept Answer" and upvote it, as this could help other community members looking for similar queries.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  3. Viorel 122.6K Reputation points
    2021-07-26T06:13:00.82+00:00

    Try this function:

    convert(nvarchar(50), Doc_Date, 103)

    The format function can be used too.

    Check the CSV file using Notepad. If you are viewing it using other programs such as Excel, then you will probably have to adjust the cell formats in Excel. It is not a BCP problem.

    0 comments No comments

  4. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-07-26T07:10:31.433+00:00

    So where are you looking at the CSV file? The image you post seems to come from Excel. Look at the CSV file from Notepad instead. Excel has its own ideas and is often too smart for its own good.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.