我尝试将字符串转换为t-sql中的日期。但是得到的结果是我无法解释的。
DECLARE @String as char(11)
DECLARE @TString as char(11)
SELECT @String = SUBSTRING([Flat File Source Error Output Column],1,CHARINDEX(',',[Flat File Source Error Output Column])-6)
FROM [ERROR].[Import_V2X]
SELECT @TString = '12/18/2009'
-- Check content before conversion
SELECT @TString as 'CheckTString'
SELECT @String as 'CheckString'
-- Convert the strings to date
SELECT CONVERT(date,@TString,101) as 'ConvertSuccess'
SELECT CONVERT(date,@String,101) as 'ConvertFails'
[平面文件源错误输出列]在表
中定义为文本这给了我以下结果:
CheckTString
------------
12/18/2009
(1 row(s) affected)
CheckString
-----------
12/18/2009
(1 row(s) affected)
ConvertSuccess
--------------
2009-12-18
(1 row(s) affected)
ConvertFails
------------
Msg 241, Level 16, State 1, Line 16
Conversion failed when converting date and/or time from character string.
任何人都可以向我解释问题所在或来自哪里? 对我来说,字符串看起来完全一样:(
答案 0 :(得分:1)
如果我不得不猜测它是因为你导入的字符串在字符串的末尾有一个不可见的字符,不允许它转换。你的变量是char(11),但字符串'12 / 18/2009'只有10个字符,所以最后留下了1个字符的空间。
答案 1 :(得分:1)
通过输出的外观,您在checkstring变量中有换行符。如果这不仅仅是问题中的复制和粘贴错误,那么将导致您所描述的错误。见下文
DECLARE @TString as char(11)
SELECT @TString = '
12/18/2009'
-- Check content before conversion
SELECT @TString as 'CheckTString'
-- Convert the strings to date
SELECT CONVERT(date,@TString,101) as 'ConvertFails'
给出以下结果。
(1 row(s) affected)
Msg 241, Level 16, State 1, Line 13
Conversion failed when converting date and/or time from character string.
答案 2 :(得分:0)
看起来@CheckString可能在开头有一个换行符。