SQL 2000 User-Defined Function .."cdate"

   (@input varchar(50))
RETURNS smalldatetime

DECLARE @Outputdate as Smalldatetime
SET @Outputdate = CAST(@input as smalldatetime)

IF @@error = ''

RETURN @Outputdate

SET @Outputdate = CAST('1/1/1900' as smalldatetime)

RETURN @outputdate

Open in new window

..trying to figure out how to "try" to cast a date, and if it errors out, then just return '1/1/1900'

I have lots of tables with lots of records .. where date of birth is not always proper.
I need to skip these records.. and can't get this working in a reliable way.
So far.. it bombs only on "out of range" dates.. and seems to be working for empty dates, and improperly formatted dates.
LVL 25
Ron MalmsteadInformation Services ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Copy this code that uses the IDSATE() function into your SSMS and let 'er rip..
Declare @input varchar(50), @outputdate datetime

SET @input = '2013-01-01'   -- 'banana', '2013-02-29'

IF ISDATE ( @input ) = 1
	SELECT 'It''s a date...'
	SET @outputdate = CAST(@input as datetime) 
	SELECT 'No date for you...'
	SET @outputdate = '1900-01-01'
SELECT @outputdate 

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ron MalmsteadInformation Services ManagerAuthor Commented:
..still getting ..
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value

I believe this is when I have a DOB like this...
..proper format.. so ISDATE() passes it.. but it's "out of range".
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Shouldn't, as 1/80/2013 will not convert to a date.
-- These return a 0, i.e. bad
SELECT ISDATE('1/80/2013')
SELECT ISDATE('2/30/2013')
-- This returns a 1, i.e. good

Open in new window

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
So ... how's it going?

Also, TechNet only has documentation on ISDATE() starting in version 2005, so if you are using SQL2K then we have to come up with an alternate solution.
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Disregard, found the documentation on ISDATE() for SQL2K, but it doesn't directly speak to bad dates such as '02-31-2013'
Ron MalmsteadInformation Services ManagerAuthor Commented:
..the out f range exception cannot be avoided or ignored apparently.
So I had to manually filter out bad ones myself.

The only other way I can think of in 2k is to use cmdshell and pass it through my own utility, but I didn't want to put that much effort into it.. so..it is what it is. (stupid users)
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
Usually stuff like this is better handled in the front-end, so I'd forward this issue to whoever's responsible for that.

Either that or use it as justification to ugrade to a more current version of SQL Server.

Thans for the grade.  Good luck.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.