?
Solved

SQL Stored procedure converting date to week number as variable

Posted on 2009-04-06
9
Medium Priority
?
567 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 1000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

764 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