Convert SmallDateTime question

I want to return all the records less than a cetain date(MM/dd/yyy). I thought the following query would do that but it does not. The query returns a record for 01/09/2009 which is grater than 11/18/2008.

Select Convert(varchar(10),EndDt,101)
 From Vacation
where Convert(varchar(10),EndDt,101) < '11/16/2008'

This returns 01/02/2009.

I chose to use the convert function in the where because I want to ignore hour, minutes and seconds. Would someone please explain why this query returns a record which seems to be incorrect.
thanks,
pat
mpdillonAsked:
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.

chapmandewCommented:
Select Convert(varchar(10),EndDt,101)
 From Vacation
where EndDt < '11/16/2008'
0
mpdillonAuthor Commented:
Ok, I think I understand. The convert function changes the date to a string. And a string that begins with 11/ is greater than a string that begins with 01/.

So then how would should I compare dates?

Select EndDt From Vaction where Enddt <= '12/01/2008' will return 12/01/2008 00:00 but will not return 12/01/2008 01:00. How do I write a Where statement that only compares the date portion of the date and ignores the Hours and minutes?
0
chapmandewCommented:
Ok, I gotcha.  try this:

Select Convert(varchar(10),EndDt,101)
 From Vacation
where Convert(varchar(10),EndDt,101) < convert(varchar(10), '11/16/2008', 101))
0
The Five Tenets of the Most Secure Backup

Data loss can hit a business in any number of ways. In reality, companies should expect to lose data at some point. The challenge is having a plan to recover from such an event.

mpdillonAuthor Commented:
Sorry didn't work for me. I think it is the string comparison. So let me ask the question differently, how do you select all the dates less than or equal to 12/01/2008.
This will work for a smalldatetime Where EndDt <= '12/01/2008 23:59'
if the field were a datetime field I would have to write
where EndDt <= '12/01/2008 23:59:59:99:99'
but there has to be a better way.
0
chapmandewCommented:
all dates <= 12/1/2008


select * from tablename
where datefield <= convert(varchar(10), '12/1/2008', 101)
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
mpdillonAuthor Commented:
Thanks for your perserverance. It works fine. I even tested with 01/01/2009. If any date was going to fail it would be one that lead off with 01/01. But your function returned 11/19/2208 as being less than 01/01/2009. Just what I wanted.
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.