Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Datepart (mm, '01/01/2009') -1 Expand / Collapse
Author
Message
Posted Tuesday, September 01, 2009 7:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 17, 2014 1:47 PM
Points: 146, Visits: 301
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.
Post #780676
Posted Tuesday, September 01, 2009 7:24 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 41,531, Visits: 34,448
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 2008, MVP
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

Post #780686
Posted Tuesday, September 01, 2009 7:35 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, March 17, 2014 1:47 PM
Points: 146, Visits: 301
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.
Post #780696
Posted Tuesday, September 01, 2009 7:43 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Yesterday @ 3:30 PM
Points: 41,531, Visits: 34,448
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 2008, MVP
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

Post #780702
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse