BCP日期格式

时间:2016-12-05 11:57:30

标签: sql-server bcp

我正在将日期转换为存储过程中的字符串,以发送到BCP。这有效,但CSV中的格式显示为dd-mm-yyyy,尽管使用的类型103应显示dd / mm / yyyy'(请参阅https://msdn.microsoft.com/en-us/library/ms187928.aspx

代码:

USE [Database]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[spExcelIntoTableMainReport]
(
    @tableName sysname,
    @xModuleID varchar(Max) 
)
AS
BEGIN
---------------------------------------
DECLARE @FilePath varchar(150)
SELECT @FilePath = 'C:\Sites\Reposts\' 
--------------------------------------------------------------
DECLARE @sql NVARCHAR(MAX)
---------------------------------------
DECLARE @tableCREATE NVARCHAR(2500)

SET @tableCREATE = 'CREATE TABLE ' + @tableName + '
(
[ID] VARCHAR(200),
DateFrom  varchar(100),
DateTo  varchar(100)
 )'
EXEC (@tableCREATE)
---------------------------------------
SET @sql = '
INSERT INTO ' + @tableName  + '
(
ID,
DateFrom  ,
DateTo
)
VALUES
(
''ID'',
''Date From (DD/MM/YYYY)'' ,
''Date to (DD/MM/YYYY)''
)'
EXEC (@sql)

SET @sql = '
INSERT INTO ' + @tableName  + '
(
ID,
DateFrom  ,
DateTo
)

SELECT ID,
LTRIM(RTRIM(CONVERT(varchar(10),cast(ModuleDateStart as Date),103))),
LTRIM(RTRIM(CONVERT(varchar(10),cast(ModuleDateComplete as Date),103) )),
FROM TBLRESULTS'

EXEC (@sql)

--------- Now we have a table create the CSV ----------
DECLARE @sql1 varchar(8000)
SELECT @sql1 = 'bcp DATABASE+ @tableName + ' out ' + @FilePath + ''+  @tableName + '.csv -c -t, -T -S '+ @@servername
EXEC master..xp_cmdshell @sql1  

END

0 个答案:

没有答案