SQL 2008 Query - DATE with <= value

I have a query that I want to run against SQL Reporting Services. I want to use <=DATE. I am having a problem because some falls on the date I am entering it does not show up.

EXAMPLE:  <= '5/11/2015'

My database has a line with this date (2015-05-11 10:12:16.087)  that is not showing up. How do I fi show I can use LESS THEN OR EQUAL TO DATE.
allenkentAsked:
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.

Russ SuterCommented:
You need to cast the datetime value to a date like this:

SELECT
 *
FROM
 myTable
WHERE
 CAST(myTable.theDate as DATE) <= '5/11/2015'

Open in new window

0
PortletPaulfreelancerCommented:
The best practice method is to use the following:
SELECT
 *
FROM
 myTable
WHERE ( [date_column] >= '20151105' AND  [date_column] <  '20151106' )
AND ...

Open in new window

In particular note that date literals in the format YYYYMMDD are the safest; don't use ambiguous formats like dd/mm/yyyy or mm/dd/yyyy
The method shown above avoids unnecessary use of functions (saving cpu) and it permits use of indexes if they exist on the date column
I have shown use with parentheses only to indicate that it can sometomes be necessary, particularly if your where clause has OR conditions

for more see:
"Beware of Between"

and
the best practice with date and time ranges is to avoid BETWEEN and to always use the form:

WHERE col >= '20120101' AND col < '20120201'
This form works with all types and all precisions, regardless of whether the time part is applicable.
Itzik Ben-Gan

Please also note:
sargable: Search ARGument ABLE
http://en.wikipedia.org/wiki/Sargable

Rules of thumb
Avoid applying functions on data values in a sql condition.
Avoid non-sargable predicates and replace them with sargable equivalents.
0
allenkentAuthor Commented:
Thank you that helps BUT I need this to be written in a visual studio report with the value entered by the user. A hard coded date does not work because I want it to change at the reporting level.

The issue is <= does not give the date they entered. It skips the = portion of the date.

 I could write my prompt as:  
ENTER END DATE - REPORT DOES NOT INCLUDE DATE YOU ENTERED:
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Russ SuterCommented:
Then you can cast both arguments as a date value

...
WHERE
 CAST([myTable].[myDate] as DATE) <= CAST(@anyDateTimeVariable as DATE)

This will absolutely ignore any time values on either side and compare dates ONLY
0
PortletPaulfreelancerCommented:
Do not use functions on data to suit a filtering predicate, please don't learn that

Declare @datein as date

Select  .....
From .....
Where ( datecol >= @datein AND datecol < dateadd(day,1,@datein )
...

You don't have to hardcore the date and you can easily add 1 day to that value
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
Russ SuterCommented:
Paul, I don't think that would work very well. It would have some unsatisfactory side effects.

What if @datein was 'Jul 12 2015  7:39PM'?

Your where clause now effectively chooses rows between

Jul 12 2015  7:39PM and Jul 13 2015  7:39PM.
0
PortletPaulfreelancerCommented:
datein is defined as date so it does not carry a time component
0
PortletPaulfreelancerCommented:
the parameter is defined as a date
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.