I have a function to convert a string to datetime (101) format.
But it gives me an error when I convert this value.
2016-03-01 00:00:00.0000000
And the error is
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
And my function is ..
ALTER FUNCTION [dbo].[ConvertToDate]
(
@Value nVarchar(MAX)
)
RETURNS DATETIME
AS
BEGIN
IF (@Value <> NULL OR @Value <> '')
BEGIN
DECLARE @dt DATETIME
SET @dt = CONVERT(DATETIME,@Value,101)
--SET @dt = CAST(@Value AS DATETIME(101))
RETURN @dt
END
RETURN NULL
END
What is the problem?
@Value <> NULL. If you compare anything withNULLusing <> the result isNULL, and not true or false. The correct way is@Value IS NOT NULL