Catch invalid values when converting number to date

I am using SQL Server 2008

I have a numeric field in the following format: yyyymmdd

I wish to convert to date in this format: mm/dd/yyyy.

However, sometimes the field contains values that cannot be converted to dates.  (examples: 0, 99999999).  I wish to have these values come through as empty value.

How can I accomplish this?

Thanks for any help.
mak345Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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

Vitor MontalvãoMSSQL Senior EngineerCommented:
You can use ISDATE to verify if the value is a date:
UPDATE YourTableName
SET NumericFieldName = NULL
WHERE ISDATE(CAST(NumericFieldName AS VARCHAR))=0

Open in new window

0
mak345Author Commented:
I don't want to update the table, I just want to create a query that will result in an empty value where applicable.  The value in the table is ok.
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
Use the same idea:
SELECT Field1, Field2, FieldN, 
                         CASE ISDATE(CAST(NumericFieldName AS VARCHAR))
				WHEN 0 THEN ''
				ELSE CAST(CAST(NumericFieldName  AS VARCHAR) AS DATE)
			END MyDate
FROM YourTableName

Open in new window

0
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

mak345Author Commented:
Thanks, but I pasted your code in and it still results in 1900-01-01, and not empty value.

CASE ISDATE(CAST(MaturityDate AS VARCHAR)) WHEN 0 THEN '' ELSE CAST(CAST(MaturityDate AS VARCHAR) AS DATE) END
0
Vitor MontalvãoMSSQL Senior EngineerCommented:
That's because the column now is date. What about returning NULL instead?
SELECT Field1, Field2, FieldN, 
                         CASE ISDATE(CAST(NumericFieldName AS VARCHAR))
				WHEN 0 THEN NULL
				ELSE CAST(CAST(NumericFieldName  AS VARCHAR) AS DATE)
			END MyDate
FROM YourTableName

Open in new window

0

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
mak345Author Commented:
PERFECT!!!

THANKS!
0
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 2008

From novice to tech pro — start learning today.