Advertisement

04.03.2008 at 08:49AM PDT, ID: 23293204
[x]
Attachment Details

Find dates associated with Week # returned from DatePart(wk,DATEVALUE) function

Asked by SWRO in MS SQL Server

Tags: Microsoft, SQL Server, T-SQL

I am trying to find the minimum and maximum date values associated with a specific week # that is returned from the DatePart(wk,SomeDate) function.

I would guess that a function would be the best bet. Anyway, what I'd like to do is pass in a week # - let's say that I have a sale that occurred on 1/15/08 and if I leave Set DateFirst at 7 I would get a value of 3 from that function - and get a week range label (varchar) in return. That label should reflect the lower and upper bounds of week #3 in tems of actual dates. So, the output should be something (I can always tinker with the formatting) like this:
1/13/2008 - 1/19/2008





Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
-- Here's sort of what I have in mind
CREATE FUNCTION GetWeekRange (@week_number smallint)
	
RETURNS VARCHAR(25)
 
BEGIN
	DECLARE @week_range varchar(25)
		,@min_range datetime
		,@max_range datetime
 
	-- PSEUDO
	-- Do something here though I have
	-- No idea how to get the range values
	SET @min_range = SELECT Something WHERE Something 
	SET @max_range = SELECT Something WHERE Something
		
	SET @week_range = CONVERT(varchar,@min_range,101) + ' - ' + CONVERT(varchar,@max_range,101)
 
	RETURN @week_range
END
 
Loading Advertisement...
 
[+][-]04.03.2008 at 09:12AM PDT, ID: 21274087

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: MS SQL Server
Tags: Microsoft, SQL Server, T-SQL
Sign Up Now!
Solution Provided By: momi_sabag
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.03.2008 at 10:55AM PDT, ID: 21275101

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.03.2008 at 12:08PM PDT, ID: 21275848

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628