| Author |
Topic |
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2009-01-11 : 11:23:39
|
| Hi i need to pass 2 ints to another stored proc@month int@year inti have a date variable @NewDate which i need to get the month and year from.i am not sure how to do this |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2009-01-11 : 11:43:34
|
| set @month = datepart(month,@NewDate);set @year = datepart(year, @NewDate);So, you could call your stored procedure using:exec yourStoredProc @month = datepart(month,@NewDate), @year = datepart(year, @NewDate); |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-11 : 11:45:18
|
| set @month = Month(@NewDate);set @year = YEAR(@NewDate); |
 |
|
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2009-01-11 : 12:46:05
|
| ok thanks that worked, just one more thing, i have this querySELECT sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Monthwhich returns3000, 1what i need is to return every month with rcount = 0 ie3000, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 12 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-11 : 12:51:59
|
quote: Originally posted by craigmacca ok thanks that worked, just one more thing, i have this querySELECT sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Monthwhich returns3000, 1what i need is to return every month with rcount = 0 ie3000, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 12
for that you need a tally table. something likeSELECT d.Month,COALESCE(rcount,0) AS countvalueFROM(SELECT number AS MonthFROM master..spt_values WHERE type='p'AND number BETWEEN 1 AND 12)mLEFT JOIN (SELECT sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Month)dON d. Month=m.Month master..spt_values is an internal count table of sql server table which i've used for purpose |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-11 : 13:06:11
|
| If you have month column with entries from 1-12Select t.Month,coalesce(rcount,0)as rcountfrom(select distinct Month from dbo.Sms_Archive_Month)tleft outer join(SELECT Sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Month)mon t.Month = m.Month |
 |
|
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2009-01-11 : 13:26:11
|
| thanks visakh16i have tried your surgestion, which works but i need the month column to show 1 - 12 currently it shows3000, 10, NULL0, NULL0, NULL0, NULL0, NULL0, NULL0, NULL0, NULL0, NULL0, NULL0, NULL |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-11 : 13:33:51
|
| Did you use coalesce function as visakh suggested? |
 |
|
|
craigmacca
Posting Yak Master
142 Posts |
Posted - 2009-01-11 : 13:53:05
|
| yes i copied it totallybut the month column shows Null values |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-01-11 : 14:08:42
|
quote: Originally posted by visakh16
quote: Originally posted by craigmacca ok thanks that worked, just one more thing, i have this querySELECT sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Monthwhich returns3000, 1what i need is to return every month with rcount = 0 ie3000, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 12
for that you need a tally table. something likeSELECT m.Month,COALESCE(rcount,0) AS countvalueFROM(SELECT number AS MonthFROM master..spt_values WHERE type='p'AND number BETWEEN 1 AND 12)mLEFT JOIN (SELECT sum(SmsAmountSent) as rcount, MonthFROM dbo.Sms_Archive_MonthWHERE SmsYear = 2009GROUP BY Month)dON d. Month=m.Month master..spt_values is an internal count table of sql server table which i've used for purpose
|
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-12 : 02:17:10
|
| ah...i had alias wrongthanks for catch Sodeep |
 |
|
|
|
|
|