Solved

Find Monday in iso_week 1

Posted on 2015-02-20
2
74 Views
Last Modified: 2015-02-20
Hi!

I need a sql query to provide me the Monday in iso_week 1 for any given year.

in 2015 it should return '2014-12-29'
in 2016 it should return '2016-01-04'
in 2017 it should return '2017-01-02'

Thanks,
0
Comment
Question by:intimenordic
[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
2 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40620857
Either:

select dateadd(day,2-datepart(dw,'20130101')+iif(datepart(dw,'20130101')>5,7,0),'20130101') as ISOWeek1

Open in new window


or, if you want to define it once:

declare @year as char(4)
set @year = '2027'

select dateadd(day,2-datepart(dw,@year + '0101')+iif(datepart(dw,@year + '0101')>5,7,0),@year + '0101') as ISOWeek1

Open in new window

0
 

Author Closing Comment

by:intimenordic
ID: 40620863
Perfect! Just used getdate for declaring, since this is a runtime thing.

declare @year as char(4)
set @year = year(getdate())
select dateadd(day,2-datepart(dw,@year + '0101')+iif(datepart(dw,@year + '0101')>5,7,0),@year + '0101') as ISOWeek1

Thanks, and have a great weekend!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

690 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