0

Is it possible to use a function like DATEADD() or DATEPART() on a varchar column that may or may not hold something that looks like a datetime?

Currently I'm trying this by using a WHERE clause to select only rows that have datetime values in this column and getting the usual arithmetic overflow error.

I could perform some sort of operation to extract just the datetime values and put them in a temp table but I'm curious about this.

Example

SELECT DATEPART(wk, DateCreated) FROM tblOrders WHERE colType = 'datetime'

Don't worry about the table design, it's not as bad as that.

1 Answer 1

6

Don't worry about the table design, it's not as bad as that.

Seems unlikely, but...

Use IsDate

Sign up to request clarification or add additional context in comments.

2 Comments

A little punch and a a proper answer in just three lines... :-)
I never even thought of using IsDate(). But still, I couldn't use DATEPART() on the column with this in my WHERE clause. Ended up going the temp table route anyway because, as it turns out, the table design sucks harder than I initially realised. Also edited my post after some frustration in creating a report today.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.