Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

convert datetime gives me errors

Status
Not open for further replies.

crystalhelp

Programmer
May 13, 2002
56
0
0
CA
The following statement works in some spots and not in others: convert(datetime,b.dateval) >= DateADD(year,-@Years,GetDate()) and convert(datetime,b.dateval) <= getdate()

b.dateval is a char and @years is an int.

It works when I have it in a basic where clause, but it doesn't work if I have it in a where clause that is 3 where clauses deep. That is the only difference I can figure out. The error I'm getting is Server: msg 242 - The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

Do you know why?
 
You have data in there which is not real date data and it cannot convert it.

Questions about posting. See faq183-874
 
I've thought of that. Except, when I replace

convert(datetime,b.dateval) >= DateADD(year,-@Years,GetDate()) and convert(datetime,b.dateval) <= getdate()

with

DATEDIFF(day, convert(datetime,b.dateval), getdate()) <= @Years*365)

which also has the convert date statement, it doesn't give me the error, which made me think the error message was wrong.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top