Solved

SQL Stored procedure converting date to week number as variable

Posted on 2009-04-06
9
534 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
  • 5
  • 3
9 Comments
 
LVL 12

Expert Comment

by:udayakumarlm
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
 
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

by Mark Wills PIVOT is a great facility and solves many an EAV (Entity - Attribute - Value) type transformation where we need the information held as data within a column to become columns in their own right. Now, in some cases that is relatively…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now