I try to convert a string into a date in t-sql. However get results that I can't explain.
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'
[Flat File Source Error Output Column] is defined as text in the table
This gives me the following result:
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.
Anybody can explain me where the problem is or comes from ? For me the strings look exactly the same :(