Solved

SQL Stored procedure converting date to week number as variable

Posted on 2009-04-06
9
520 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
Comment Utility
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
Comment Utility
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
Comment Utility
--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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:cbads
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

17 Experts available now in Live!

Get 1:1 Help Now