Author |
Topic |
senilssonli
Starting Member
7 Posts |
Posted - 2004-09-16 : 03:46:31
|
I have built a stored procedure, where I can send in a data and then get back all days in that month. Now I want the result in the same result set. Is there a way of doing that?----------------------------------------------------------------CREATE PROCEDURE sp_GetAllDaysOfMonth@input_date datetime='2004-09-01'asdeclare @counter int set @counter=0set datefirst 1while datepart(month,@input_date)=datepart(month,DATEADD(day,@counter,@input_date))beginselect datepart(year,DATEADD(day,@counter,@input_date)) as year, datepart(month,DATEADD(day,@counter,@input_date)) as Month, datepart(day,DATEADD(day,@counter,@input_date)) as DayInMonth, datename(month,DATEADD(day,@counter,@input_date)) as MonthName, datepart(weekday,DATEADD(day,@counter,@input_date)) as DayInWeek, datepart(week,DATEADD(day,@counter,@input_date)) as WeekNo, datepart(dayofyear,DATEADD(day,@counter,@input_date)) as DayInYearset @counter=@counter+1endGO |
|
samsekar
Constraint Violating Yak Guru
437 Posts |
Posted - 2004-09-16 : 04:34:42
|
Can USE Temporary Table.----------------------------------------------------------------CREATE PROCEDURE sp_GetAllDaysOfMonth@input_date datetime='2004-09-01'asdeclare @counter int set @counter=0set datefirst 1Create table #temp (t_year int, T_Month int, DayInMonth int, MonthName varchar(20), DayInWeek int, WeekNo int, DayInYear int )while datepart(month,@input_date)=datepart(month,DATEADD(day,@counter,@input_date))beginInsert into #temp (t_year, T_Month, DayInMonth, MonthName, DayInWeek, WeekNo, DayInYear )select datepart(year,DATEADD(day,@counter,@input_date)) as year,datepart(month,DATEADD(day,@counter,@input_date)) as Month,datepart(day,DATEADD(day,@counter,@input_date)) as DayInMonth,datename(month,DATEADD(day,@counter,@input_date)) as MonthName,datepart(weekday,DATEADD(day,@counter,@input_date)) as DayInWeek,datepart(week,DATEADD(day,@counter,@input_date)) as WeekNo,datepart(dayofyear,DATEADD(day,@counter,@input_date)) as DayInYearset @counter=@counter+1endSelect * from #tempdrop table #tempGO- Sekar |
|
|
samsekar
Constraint Violating Yak Guru
437 Posts |
Posted - 2004-09-16 : 04:36:25
|
Also, Can use DECLARE @mytable (t_year int, T_Month int..- Sekar |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2004-09-16 : 08:25:07
|
If you have a tally ( numbers ) table it can be handy,here I create one on the fly...SET NOCOUNT ONSET DATEFIRST 1DECLARE @input_date DATETIMEDECLARE @end_date DATETIME-- 1st day of this monthSET @input_date = DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0)-- last day of this monthSET @end_date = DATEADD(MONTH,1,@input_date)- 1-- create a tally table with all days from start to end...CREATE TABLE #tally(daynr INT NOT NULL)INSERT #tallySELECT CAST(@input_date AS INT)WHILE ( SELECT MAX(daynr) FROM #tally ) < CAST( @end_date AS INT ) -- cast not really necessary INSERT #tally(daynr) SELECT MAX(daynr) + 1 FROM #tally-- select the dateinfo from tally tableSELECT YEAR(daynr) AS [Year], MONTH(daynr) AS [Month], DAY(daynr) AS [DayInMonth], DATEPART(MONTH,daynr) AS [MonthName], DATEPART(WEEKDAY,daynr) AS [DayInWeek], DATEPART(WEEK,daynr) AS [WeekNo], DATEPART(DAYOFYEAR,daynr) AS [DayInYear]FROM #tallyDROP TABLE #tally rockmoose/* Chaos is the nature of things...Order is a lesser state of chaos */ |
|
|
senilssonli
Starting Member
7 Posts |
Posted - 2004-09-16 : 14:28:55
|
Thank you guys, works very well. Now I know about temporary tables and the "SET NOCOUNT ON" was very handy. /Lars |
|
|
|
|
|