Sunday, March 11, 2012

Code to verify date as Valid

Is there code to establish if a date is valid
so that I can send an error message if i get
the 31st of February for instance
im thinking of something like the following but don't know the code
@.feedback = case when (@.date is ok? ) then 'DateGood' else 'DateBad' end
regards EarnieI assume that @.date is a character string? If it was of a datetime datatype,
it would not have accepted an invalid date.
So, if @.date is a character string, you can use the ISDATE() function:
declare @.validdate as varchar(30), @.invaliddate as varchar(30);
set @.validdate = '20050212';
set @.invaliddate = '20050231';
select isdate(@.validdate), isdate(@.invaliddate)
-- --
1 0
BG, SQL Server MVP
www.SolidQualityLearning.com
"Earnie" <Earnie@.discussions.microsoft.com> wrote in message
news:D7CB4DC0-D8B0-433F-9D77-E40D3D147866@.microsoft.com...
> Is there code to establish if a date is valid
> so that I can send an error message if i get
> the 31st of February for instance
> im thinking of something like the following but don't know the code
> @.feedback = case when (@.date is ok? ) then 'DateGood' else 'DateBad' end
> regards Earnie|||ISDATE function
e.g. from BOL:
DECLARE @.datestring varchar(8)
SET @.datestring = '12/21/98'
SELECT ISDATE(@.datestring)
Regards
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"Earnie" <Earnie@.discussions.microsoft.com> wrote in message
news:D7CB4DC0-D8B0-433F-9D77-E40D3D147866@.microsoft.com...
> Is there code to establish if a date is valid
> so that I can send an error message if i get
> the 31st of February for instance
> im thinking of something like the following but don't know the code
> @.feedback = case when (@.date is ok? ) then 'DateGood' else 'DateBad' end
> regards Earnie|||use isdate(expr)

No comments:

Post a Comment