Your field that you are comparing it to needs to have the time portion stripped out. To strip out the time portion and allow for date only comparison....
where convert(datetime,convert(c
Main Topics
Browse All TopicsI'm using Business Intelligence Development Studio in Visual Studio 2005 environment.
When I preview a report with the parameter dates of 7/3/08 to 7/3/08 I get no results.
When I use dates of 7/3/08 to 7/4/08 I get 5 records dated 7/3/08. Because of help from EE I understand why it's happening but need help with the code. I've attached what it should be logically but I get the error...
Cannot compare data of types System.DateTime and System.String
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thank you both for your quick responses. I tried making changes to my code using Brandon's suggestion because it made more sense with my existing code. But I admit I'm new to SQL and I've gotten EE help on the code I have so far so I'm unsure of my changes. It complies and runs in data view but when I try to preview the report I get the same error. Is what I'm trying to do not possible with a filter? Do I have to use a parameter?
Thanks again!
Gladly.
I actually gave you 2 snippets. One is because I was using GetDate and EE advised me that GetDate uses time, so change to convert.
The 2nd snippet includes convert changes I added which may explain why it doesn't look quite right. Since it gave the same final result I thought it was working but I admit it's hard to read.
Your help is greatly appreciated.
I didn't mean to scare you away. I thought your code was helpful. I took out the GETDATE in my code and added your code and I get the results I want...Thanks! It made sense that insteasd of getting the last 3 months and the narrowing it down from there that I should just get exactly what I need. That way it's faster...well so I thought. With the GETDATE it takes about 30 seconds to run which I thought was kind of long. Without that it takes almost 3 minutes. Shouldn't it take less time? Can you take another look at my newly posted code. I'd really appreciate it.
Thanks!
I'm glad you are back. I'll try to answer all your questions.
Ques 1. You are correct. I think it should be faster but that is based on the fact that with GETDATE is 32 seconds and without is over 3 minutes. I thought my just picking specific data it should be faster. Therefore I figure it's something I did in my code.
2. It may be easier if I give you a link to my previously posted question.
http://www.experts-exchang
3. Yes there is an index on dbo.users.name
4. I think the link should clarify this as well.
5. I will try your posted code and let you know. Thank you so much
Thank you so much for your continued help. I tried your suggestion but it still does not display any results.
However, the code I've attached is giving me the data I want and it's fast so I will use it. Even tho I don't know why I need the following portion of the code. It works without it but really really slow. It appears I'm grabbing 3 months of data and then selecting one date from that data. Shouldn't it be faster to just select the one date?
Oh well. Please advise if you know why but I will go ahead and award points since it works.
Thanks again.
and g.Timetag BETWEEN DATEADD(m, - 3, CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
AND CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120)
AND g.Timetag BETWEEN DATEADD(m, - 3, GETDATE()) AND
CONVERT(DATETIME, CONVERT(VARCHAR(10),
CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120), 120), 120)
Business Accounts
Answer for Membership
by: simonetPosted on 2008-09-17 at 09:51:48ID: 22500873
That's because the Datetime fields contains a decimal part that holds the hour/minute/second part of the date.
When comparing dates, if you're interested only in the date part (regardless of the time part), you can have a where clause like this:
WHERE (cast(cast(SomeDateField as int) as datetime) = '3 JUL 2008')
if you want to compare intervals of time:
WHERE (cast(cast(SomeDateField as int) as datetime) between '3 JUL 2008' and '07 JUL 2008')
This will yield correct results.
In short, what you need to do when comparing dates, is force SQL Server to ignore the time component of the datetime field, so that the comparisions are accurate.
Alex