Solved

Find Monday in iso_week 1

Posted on 2015-02-20
2
62 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
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sql query 7 35
Shop sales Actual vs Forecast 2 26
how to fix this error 14 46
Calculating Business Hours 19 62
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now