Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Print a DayName without using Date functions

$
0
0

Hi,

I have an assignment like without using any date functions i should print a calendar.

Below is the code without using any datefunctions like dateadd, datediff, datename a calendar has been generated for month and year entered. I want a week name for the dates like sunday ... monday etc. 

I can take any date from calendar as reference  and calculate based on that date.

ex: today is 2/20/2014 thursday . Next 7days again will be thursday, same way before 7days will be thursday.

I need to loop in below procedure and get weekname. 

Plz help in the code,

I am using SQL server 2008

IF OBJECT_ID ('dbo.Calendar1') IS NOT NULL
     DROP PROCEDURE dbo.Calendar1
GO

CREATE  PROCEDURE [dbo].Calendar1 --4,1991

 (
   @month int,
   @Year  int
 )
 AS  
 BEGIN
 declare 

 @startdateofMonthYear date,
 @EnddateofMonthYear Date


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 DATE) Calendardates

UNION ALL
SELECT   daysint+1,Cast(SUBSTRING(cast(@startdateofMonthYear as varchar(20)),1,7) + '-'+CAST(daysint+1 as varchar(2)) as DATE) Calendardates
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

 

bhavana



Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>