我收到此错误"从字符串转换日期和/或时间时转换失败。"

时间:2014-09-18 16:43:59

标签: ssis

DECLARE @Date DATE
DECLARE @FileName VARCHAR(1000)
DECLARE @FilePath VARCHAR(8000)

SET @FilePath = 'S:\FFgh\ENG\dataload'
SELECT @FileName = REVERSE(SUBSTRING(REVERSE(REPLACE@FilePath,'.DAT','')),1,CHARINDEX '\',REVERSE(REPLACE(@FilePath,'.DAT','')))-1))

SELECT @Date = CAST(Date AS DATE) FROM FileHeader Where FinalName = @FileName

SELECT CAST(@Date AS DATETIME) AS Date, @@ROWCOUNT AS HeaderFooter

这是我的代码,如果我使用文件名而不是@FileName它是有效的,否则会给我错误。

1 个答案:

答案 0 :(得分:0)

REPLACECHARINDEX找不到一些括号。在下面添加..

DECLARE @Date DATE
DECLARE @FileName VARCHAR(1000)
DECLARE @FilePath VARCHAR(8000)

SET @FilePath = 'S:\FFgh\ENG\dataload'
SELECT @FileName = REVERSE(SUBSTRING(REVERSE(REPLACE(@FilePath,'.DAT','')),1,CHARINDEX ('\',REVERSE(REPLACE(@FilePath,'.DAT','')))-1))

SELECT @Date = CAST(Date AS DATE) FROM FileHeader Where FinalName = @FileName

SELECT CAST(@Date AS DATETIME) AS Date, @@ROWCOUNT AS HeaderFooter