开发者

Try/Catch in UDF not possible? How to "TryCast" a varchar to datetime?

how can i convert a varchar parameter into datetime and if cast fails use GetDate() as default? I've tried to put it in a Try/Catch but apparently that doesn't work in a UDF. It also does not work to simply check if the datetime is null, because it'll throw an exception('The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value'):

CREATE FUNCTION [dbo].[getRZUInfo]
(
    @IMEI varchar(20),
    @StrDeliveryDate varchar(20)
)
RETURNS VARCHAR(8000)
AS
BEGIN
    DECLARE @Info VARCHAR(8000) 
    DECLARE @DeliveryDate datetime;
    SET @DeliveryDate = Convert(datetime,@StrDeliveryDate,102);
    IF @DeliveryDate IS NULL
        SET @DeliveryDate=GetDa开发者_如何学编程te();
    SELECT   @Info = COALESCE(@Info + '|', '') + 'TAT_B2B: ' + Convert(varchar,tabData.TAT_B2B) + ', AC' + Convert(varchar,tabData.fimaxActionCode) + ', Diff: ' + Convert(varchar,DateDiff(day,tabData.Received_date,@DeliveryDate))
        FROM   tabData
    WHERE     (SSN_Number = @IMEI) AND (Received_Date >= DATEADD(month, -3, @DeliveryDate))
    ORDER BY SSN_Number,Received_Date DESC
    return @Info
END


SET @DeliveryDate = CASE
                      WHEN Isdate(@StrDeliveryDate) = 1 THEN
                      CONVERT(DATETIME, @StrDeliveryDate, 102)
                      ELSE Getdate()
                    END  


A common flaw with IsDate is that it is unable to take in a date format specifier that CAST/CONVERT can.

See this:

set dateformat dmy
declare @StrDeliveryDate varchar(20) set @StrDeliveryDate = '2011.12.13'
select CASE
 WHEN Isdate(@StrDeliveryDate) = 1 THEN
 CONVERT(DATETIME, @StrDeliveryDate, 102)
 ELSE Getdate()
 END 

output: 2011-03-21 22:19:54.683

This is a better function for testing 102-formatted dates specifically. Actually 102 is much easier, this is flexible enough to pick up yy/yyyy, m/mm, d/dd.

create function dbo.Is102Date(@any varchar(50))
-- 102 = yyyy.mm.dd
returns bit as begin
set @any = ltrim(rtrim(@any))
declare @theyear varchar(10)
set @TheYear = case
       when @any like '%[^0-9.]%' then null
       when @any like '[0-9][0-9].%[0-9].%[0-9]' then
           case when LEFT(@any,2) >=50
           then '19'+LEFT(@any,2)
           else '20'+LEFT(@any,2)
           end
       when @any like '[0-9][0-9][0-9][0-9].%[0-9].%[0-9]' then
           LEFT(@any,4)
       end
declare @YYYYMMDDToTest varchar(50)
set @YYYYMMDDToTest = case
       when @TheYear is not null then
           @TheYear
           + -- month
           SUBSTRING(@any, charindex('.',@any) +1,
           charindex('.',@any,charindex('.',@any)+1)-
           charindex('.',@any)-1)
           + -- day
           right(@any,charindex('.',reverse(@any))-1)
       end
return ISDate(@YYYYMMDDToTest)
end
GO

Use it instead of ISDATE to test for 102-formatted dates in varchar.


Just checking when Null

IF @StrDeliveryDate IS NULL
    SET @DeliveryDate = GetDate();
ELSE
    SET @DeliveryDate = Convert(datetime, @StrDeliveryDate, 102);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜