question

bhavana avatar image
bhavana asked

print calendar in sql server 2008

CREATE PROCEDURE [dbo].Calendar1 --4,1991 ( @month int, @Year int ) AS BEGIN declare @startdateofMonthYear date, @EnddateofMonthYear Date if (@month >12) BEGIN Print 'Enter a valid month' END if (@month < 12) BEGIN Set @startdateofMonthYear=(Select cast(@Year as varchar(4)) +'-'+Right('00'+Cast(@month as varchar(2)),2) +'-'+'01') Set @EnddateofMonthYear = (SELECT case when @month IN (1,3,5,7,8,10,12) then cast(@Year as varchar(4)) +'-'+Right('00'+Cast(@month as varchar(2)),2) +'-'+'31' when @month IN(4,6,9,11) then cast(@Year as varchar(4)) +'-'+Right('00'+Cast(@month as varchar(2)),2) +'-'+'30' else cast(@Year as varchar(4)) +'-'+Right('00'+Cast(@month as varchar(2)),2) +'-'+(CASE WHEN (@YEAR % 4 = 0 AND @YEAR % 100 <> 0) OR @YEAR % 400 = 0 THEN '29' else '28' End) End) ;WITH CTE_DatesTable AS ( Select 1 daysint, Cast(SUBSTRING(cast(@startdateofMonthYear as varchar(20)),1,7) + '-'+CAST(1 as varchar(2)) as varchar) Calendardates --,@startDayIndex as Level UNION ALL SELECT daysint+1,Cast(SUBSTRING(cast(@startdateofMonthYear as varchar(20)),1,7) + '-'+CAST(daysint+1 as varchar(2)) as varchar) Calendardates --,Case when CTE_DatesTable.Level = 7 then 1 else CTE_DatesTable.Level+1 end FROM CTE_DatesTable WHERE daysint<= (SELECT case when @month IN (1,3,5,7,8,10,12) then 31 when @month IN(4,6,9,11) then 30 else (CASE WHEN (@YEAR % 4 = 0 AND @YEAR % 100 <> 0) OR @YEAR % 400 = 0 THEN 29 else 28 End) End)-1 ) Select [DWDateKey]=Calendardates, [DayDate]=daysint, [MonthNumber]=@Month, [MonthName]=Case when @month =1 then 'January' when @month =2 then 'February' when @month =3 then 'March' when @month =4 then 'April' when @month =5 then 'May' when @month =6 then 'June' when @month =7 then 'July' when @month =8 then 'August' when @month =9 then 'September' when @month =10 then 'October' when @month =11 then 'November' when @month =12 then 'December' End, [Year]=@Year From CTE_DatesTable END END In the below code i want to get a week name like sunday monday,,without using any date functions like datediff,dateadd,datename without using any of these i should get a weekname. So i got a hint like i know today date 2/13/2014 , 7 days before it was feb 6th that is thursday. Again 7 days before feb 6th is Jan 30th and it is also thursday ,In same way after feb 13th its feb 20th will be thursday. Based on this logic can we get? without using date functions. Only i shud pass month and year as parameter. Is it possible? Plz help
calendar
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
simmonnd avatar image
simmonnd answered
Any reason that you don't want to use date functions? The easiest would be to use DATENAME(DW,GETDATE())?
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Thats the assignment given to me. without using any date functions i should do
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.