WHERE [CreateDate] >= '08/01/2007'
and [CreateDate] < '09/01/2007'
will be much faster, especially if you have an index on CreateDate.
Main Topics
Browse All TopicsHello everyone
I have a query that has a where clause and in my where clause I am using
WHERE DATEPART(mm, [CreateDate]) = DATEPART(mm, '8/01/2007)
AND DATEPART(yyyy, [CreateDate]) = DATEPART(yyyy, '8/01/2007)
I would like to shorten my query by using less code. Is there a way I can do the same thing with less code?
Thanks for any assistance
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.
Try:
WHERE CreateDate >= '20070801' AND CreateDate < '20070901'
The 'YYYYMMDD' format for date literals is "safe" irrespective of date format and language settings. Avoid any expressions based on the date column otherwise the server will have to compute them for each row and will be less able to take advantage of any indexes.
The suggestions are checking to see if the CreateDate is any day in the Month of August 2007. The fact that they utilized the first of September is simply to make sure that the CreateDate is on or AFTER 8/1/2007 (>= '8/1/2007'), but simultaneously BEFORE 9/1/2007 (< '9/1/2007'). You code is testing ALSO if the MonthPart of the date is 8 (August), and the year part of the date is 2007 (which means that you are ALSO testing if the CreateDate is any date in the Month of August, 2007. All of the suggesed alternatives ar testing EXACTLY the same thing that your code is testing, and the offered suggestions are MUCH easier to understand, an will, in fact., asll execute FASTER than your original code.
AW
The code you have only checks Month and year, which is why everyone assumed you want anything in August 2007. If you just want that one date, this will work
CONVERT(DATETIME, [CreateDate], 101) = '08/01/2007'
If you want anything in August 2007, see other posts or
year ([createdate]) = 2007
and month (createdate]) = 8
>Thanks for the respone but sorry, my compare date is only '8/1/2007:' I am not comparing '9/1/2007'. This was not a typo. There is not a >= or <=. Thanks for any assitance.
well, you want all the records that are in the month 2007-08, yes or no?
so, if you insist on only using that value, we could rewrite the efficient query like that:
WHERE [CreateDate] >= CONVERT(DATETIME, '08/01/2007', 101)
AND [CreateDate] < dateadd(month, 1, CONVERT(DATETIME, '08/01/2007', 101))
and also here, the >= and the < are not a type neither.
the shorter version would be like this:
WHERE CONVERT(VARCHAR(7), [CreateDate], 120) = CONVERT( VARCHAR(7), CONVERT(DATETIME, '08/01/2007', 101), 120)
Business Accounts
Answer for Membership
by: angelIIIPosted on 2007-08-11 at 14:42:45ID: 19677365
less code... well not really.
this version would be able to take into consideration an index on field CreateDate:
WHERE [CreateDate] >= CONVERT(DATETIME, '08/01/2007', 101)
AND [CreateDate] < CONVERT(DATETIME, '09/01/2007', 101)
shorted would be this:
WHERE CONVERT(VARCHAR(7), [CreateDate], 120) = '2007-08'