this is very general , but complicated situation to get the input date which in proper format or not. Which ultimately gives an error at run-time , even in the production server we get the error and we rectify the issue hour and hour.
Ultimately sqlserver introduce the new function called "ISDATE" or TRY_PARSE-only sql2012 or above, which return the 1 if input data is date format.
declare @dTable table ( datecolumn datetime)
INSERT into @dTable values (case isdate('19-11-2014 10:10:41') when 1 then '19-11-2014 10:10:41' else null end )
INSERT into @dTable values ('2014-11-19 10:10:41')
INSERT into @dTable values ('11-19-2014 10:10:41')
select * from @dTable
If still you want , this is not right way, then you can create a function of dateformat, in which you can give string datevalue in any format and function give the data date or null return.
check this links.
http://www.codeproject.com/Articles/576178/cast-convert-format-try-parse-date-and-time-sql#4
create function convertStringIntoDate
(
@stringValue varchar(50)
)
RETURNS datetime
AS
BEGIN
DECLARE @datereturn datetime
set @datereturn = TRY_PARSE( @stringValue)
RETURN @datereturn
END
or you can check this logic too.
declare @dt varchar(50) = '19-11-2014 10:10:41'
declare @dTable table ( datecolumn datetime)
INSERT into @dTable values (
case
when isdate(CONVERT( varchar(50), @dt)) = 1 then CONVERT( varchar(50), @dt) --'19-11-2014 10:10:41'
when isdate(CONVERT( varchar(50), @dt, 103) ) = 1 then CONVERT( datetime, @dt , 103 ) --'19-11-2014 10:10:41'
when isdate(CONVERT( varchar(50), @dt, 102) ) = 1 then CONVERT( datetime, @dt , 102 ) --'19-11-2014 10:10:41'
--when --give other format as above given and if not set in any dateformat , then simply return null
else
null
end )
select * from @dTable