Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

IsSmallDate Function - Corrected (See related posts)

Courtesy of Mounir BEN HAMED, the following is the corrected function.
Background Info: See SQL: ISDATE(@foo) and CAST(@foo AS smalldatetime) and the incorrect Updated IsSmallDate function

Oskar Austegard
http://mo.notono.us

--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

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts