Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
craigwg
Posting Yak Master
154 Posts |
Posted - 2010-04-16 : 16:28:23
|
Hi,I am writing a stored procedure that is selecting some financial data. I am loading the data into a temp table that has two columns: "date" and "value".The proc will look at all the data and pull the last 12 months. I can statically make that happen, but how do I do it so it detects the current month and only pulls the last 12?Here is a sample of my data set:date value2010-12-01 00:00:00.000 Dec12|882372010-11-01 00:00:00.000 Nov11|896732010-10-01 00:00:00.000 Oct10|916562010-09-01 00:00:00.000 Sep09|876782010-08-01 00:00:00.000 Aug08|805482010-07-01 00:00:00.000 Jul07|830592010-06-01 00:00:00.000 Jun06|811432010-05-01 00:00:00.000 May05|779772010-04-01 00:00:00.000 Apr04|744942010-03-01 00:00:00.000 Mar03|760182010-02-01 00:00:00.000 Feb02|666602010-01-01 00:00:00.000 Jan01|680072009-12-01 00:00:00.000 Dec12|666002009-11-01 00:00:00.000 Nov11|715732009-10-01 00:00:00.000 Oct10|736172009-09-01 00:00:00.000 Sep09|721262009-08-01 00:00:00.000 Aug08|657992009-07-01 00:00:00.000 Jul07|718702009-06-01 00:00:00.000 Jun06|799052009-05-01 00:00:00.000 May05|690312009-04-01 00:00:00.000 Apr04|708992009-03-01 00:00:00.000 Mar03|813182009-02-01 00:00:00.000 Feb02|708202009-01-01 00:00:00.000 Jan01|76388 Craig Greenwood |
|
|
tosscrosby
Aged Yak Warrior
676 Posts |
Posted - 2010-04-16 : 16:54:25
|
| Look up the dateadd function in BOL. Fairly simple but if you need more details, provide us with what you've tried to accomplish your results and we'll be glad to help further.Terry-- The problem with socialism is that you eventually run out of other people’s money. -- Margaret Thatcher |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
killtacularmania
Starting Member
22 Posts |
Posted - 2010-04-19 : 10:13:41
|
| this using just a where clauseSelect * From FinancialTableWhere financialdate > CAST(CAST(YEAR(dateadd(month,-12,financialdate)) As varchar(4)) + '/' + CAST(MONTH(dateadd(month,-12,finanicaldate)) As Varchar(2)) + '/01' As Datetime)I use this all the time, it puts together the current month and 01 as the day to give you the start of each month and then it will subtract 12 months. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-04-19 : 10:50:02
|
quote: Originally posted by killtacularmania this using just a where clauseSelect * From FinancialTableWhere financialdate > CAST(CAST(YEAR(dateadd(month,-12,financialdate)) As varchar(4)) + '/' + CAST(MONTH(dateadd(month,-12,finanicaldate)) As Varchar(2)) + '/01' As Datetime)I use this all the time, it puts together the current month and 01 as the day to give you the start of each month and then it will subtract 12 months.
no need of those convertions.you could simply doWhere financialdate >dateadd(mm,datediff(mm,0,financialdate)-12,0)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|