C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Find Week in Given Date in SQL Server


Posted Date: 18 Jul 2007    Resource Type: Code Snippets    Category: SQL

Posted By: Rajaraman       Member Level: Diamond
Rating:     Points: 10



In Many caseswe need to prepare the records on weekly basis. In this case, the below function is very useful to find the week in given date.


CREATE FUNCTION DBO.FINDINWEEK (@GIVENDATE DATETIME)
RETURNS VARCHAR(15)
AS
BEGIN
DECLARE @firstDayOfMonth VARCHAR(20),
@findWeek INT,
@weeks VARCHAR(30)

SET @firstDayOfMonth = CAST(MONTH(@givenDate) AS VARCHAR(2))+'/'+'1'+'/'+ CAST(YEAR(@givenDate) AS VARCHAR(4))

SET @findWeek= DATEPART(wk, @givendate) - DATEPART(wk, @firstDayOfMonth) + 1

SET @weeks = CASE @findWeek
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
WHEN 3 THEN 'Third'
WHEN 4 THEN 'Fourth'
WHEN 5 THEN 'Fifth'
WHEN 6 THEN 'Sixth'
ELSE 'Seventh'
END

RETURN @weeks + ' Week'
END


How to call this function from Sql sever?


select dbo.FindInWeek('02/05/2007')

Output

Second Week

The result shows second week because the date 02/05/2007 falls on a Monday which is the second week, considering the Sunday to Saturday as the week.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to send&Log HTML fromated mail message from SQL Server.
Previous Resource: How to count number of columns in a SQL 2000/2005 data table.
Return to Discussion Resource Index
Post New Resource
Category: SQL


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use