Solved

SQL Stored procedure converting date to week number as variable

Posted on 2009-04-06
9
566 Views
Last Modified: 2012-05-06
Hi,
I need help writing a stored procedure that takes the date entered and converts it to a week number stored as a variable which is then passed into a sql statement. Example: user enters 4/6/2009 as a date input it returns week 15 as @week, which is then used in a select statement:
select * from dbtest where createddate = @week. Any assistance is greatly appreciated. Thanks.

Claude
0
Comment
Question by:cbads
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 12

Expert Comment

by:udaya kumar laligondla
ID: 24077786
select datediff(dd,cast(datePart(yyyy,FieldName) as varchar) + '-'+'01'+'-'+'01',FieldName ) / 7 from tablename will give you the no of weeks
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24078257
Do you want your week to represent 7 days from Sunday to Saturday of any given entered date?
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 250 total points
ID: 24078412
--Sample input date:
declare @InputDate datetime
set @InputDate = '2009-04-06'

--This removes the time component.
set @inputdate = dateadd(d, datediff(d, 0, @inputdate),0)
--This translates @Inputdate into Sunday of that week.
select @inputdate = @InputDate-(((@@datefirst+datepart(dw, @inputdate))%7)-1)

-- Now you say >= Sunday at midnight and < Next Sunday.
select * from dbtest
where createddate >= @week
and createddate  < @week-7
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24078570
FYI:  I've wrapped the logic inside of a UDF and posted it over on our site at SQL Server Nation.

http://sqlservernation.com/blogs/howtos/archive/2009/04/06/how-to-determine-the-sunday-of-a-given-week.aspx
0
 

Author Comment

by:cbads
ID: 24080340
Yes, I would like the weeks represented from Sunday to Saturday using a date entered by the user to be inputed as a variable used in a stored procedure. Thanks
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24080358
in post http:#a24078412

The last line:
and createddate  < @week-7

should be:
and createddate  < @week+7

Other than that, try what I've already posted.
0
 

Author Comment

by:cbads
ID: 24081220
Thanks, it worked perfectly but I was hoping to use the @week variable against a field that has the created date already calculated with the week number in a table...so when the user enters the start date of the week(2009-04-05)....it will convert it to a week number(15) which is then used in the select query : Select * from dbtable where columna = @week. Sorry if I didn't explain it better in the initial post.
Claude
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 24081368
So what happens if they pass January 1, 2009?  That's a Thursday.  And only January 1-3 will have Week Number 1, December 28-31 will have week 53 from the previous year.
0
 

Author Comment

by:cbads
ID: 24086352
I see your point, I guess using the week number would not work in that example. Thanks again for your help. I'll use the previous example where createddate >= @week
and createddate  < @week+7.
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question