SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Datepart (mm, '01/01/2009') -1


Datepart (mm, '01/01/2009') -1

Author
Message
Marcus Farrugia
Marcus Farrugia
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 505
I'd like to collect data for a report that goes back three months using the datepart function and month as my interval. As an example if I wrote a query the following way:


select *
from table1
where
datepart(mm, DateCol1)
between datepart(mm, getdate()) -3 and datpart(mm, getdate())



I would get the dataset I'm looking for, however; come January 2010 when the query runs


between datepart(mm, '01/01/2010') -3 and datpart(mm, '01/01/2010')



the datepart interval is going to return a negative integer
ie, select datepart(mm, '01/01/2010') -1 returns the value: -1

using the datepart(mm, getdate()) function, is there a way to go back and retrieve months from previous years?
Thank you in advance.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86406 Visits: 45232
Not datepart, but try dateadd.

p.s. the way you've written those queries is highly inefficient. By putting a function on the column, you're forcing a table scan, SQL cannot use indexes properly. Also, you can't go over the year boundries.

Try something like this rather.

select <Column List>
from table1
where
DateCol1 between dateadd(mm, -3, getdate()) AND getdate()



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Marcus Farrugia
Marcus Farrugia
Old Hand
Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)Old Hand (387 reputation)

Group: General Forum Members
Points: 387 Visits: 505
GilaMonster (9/1/2009)
Not datepart, but try dateadd.

p.s. the way you've written those queries is highly inefficient. By putting a function on the column, you're forcing a table scan, SQL cannot use indexes properly. Also, you can't go over the year boundries.

Try something like this rather.

select <Column List>
from table1
where
DateCol1 between dateadd(mm, -3, getdate()) AND getdate()



Thanks Gila, that was actually quite simple, I appreciate the insight and suggestion.
GilaMonster
GilaMonster
SSC Guru
SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)SSC Guru (86K reputation)

Group: General Forum Members
Points: 86406 Visits: 45232
See this for some tricks on working with datetime values
http://sqlinthewild.co.za/index.php/2007/11/05/datetime-manipulation/

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search