Click here to Skip to main content
11,920,641 members (57,586 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked

Find Week in Given Date in SQL Server

, 4 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
This sql function is used to find the week in Given Date.


This small article is explained, How to find a week in given date from sql server. In Many scanerio we need to prepare the records in weekly basis. In this case, the below funciton is very useful to find the week in given date.

Create Function dbo.FindInWeek (@givendate datetime)
returns varchar(15)
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'

How to call this function from Sql sever?

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


Second Week


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

I have 6+ years of experience in Microsoft Technologies and have 2+ years of experience in .NET Technologies. Currenty I am woring in Hyderabad and involed in various domain projects.

If you have any doubts please reach me:

You may also be interested in...

Comments and Discussions

GeneralCode adjustments [modified] Pin
Member 423718526-Mar-09 10:48
memberMember 423718526-Mar-09 10:48 
GeneralAnother option Pin
benoli5-Feb-07 6:49
memberbenoli5-Feb-07 6:49 
GeneralRe: Another option Pin
Rajaraman.net056-Feb-07 23:15
memberRajaraman.net056-Feb-07 23:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151120.1 | Last Updated 5 Feb 2007
Article Copyright 2007 by Rajaraman.net05
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid