mo.notono.us

Monday, April 03, 2006

SQL: Corrected IsSmallDate function

Mounir BEN HAMED was kind enough to alert me of a blatant, brain-dead bug in my IsSmallDate SQL function.

So, below is the corrected code, also to be found at Code Snippets

--Checks if a string is a valid smalldatetime
--Updated 04/03/06 by Oskar Austegard after bug find by Mounir Ben Hamed
CREATE FUNCTION dbo.IsSmallDate
(
  @SmallDateString varchar(20) --The input string to check
)
RETURNS BIT
AS
BEGIN
  DECLARE @Result bit
  SET @SmallDateString = LTRIM(RTRIM(@SmallDateString))

  IF ISDATE(@SmallDateString) = 1
   AND CONVERT(datetime, @SmallDateString) BETWEEN '1900-01-01' AND '2079-06-06'
    SET @Result = 1
  ELSE
    SET @Result = 0

  RETURN @Result
END

Tags:

Labels:

0 Comments:

Post a Comment

<< Home