Solved

ssrs return first monday of month

Posted on 2014-07-28
8
976 Views
Last Modified: 2014-07-29
Hi I am wanting to set a parameter in SSRS, Report Builder 3.0  that returns the first Monday of April in the current year.  Could anyone help me with this expression please, I cannot seem to get my head around it.
0
Comment
Question by:deborahhowson00
  • 2
  • 2
  • 2
  • +2
8 Comments
 

Author Comment

by:deborahhowson00
ID: 40223845
I want to return the date of that first Monday of April in the current year, sorry, just to be clear.
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40223953
I have an article out there called How to Build Your Own SQL Calendar Table that handles situations like this.

Otherwise, you'll probably need to build some kind of loop to start at CAST(CAST(YEAR(GETDATE) as char(4)) + '-04-01' as date), then incriment one day at a time until SELECT DATEPART(dw, {the date} ) = 1 for Monday.

So ... Create a SP that contains the below code, attach it as a data set, and in your parameter set all values to this:
Declare @dt datetime
SET @dt = CAST(CAST(YEAR(GETDATE()) as char(4)) + '-04-01' as date) 

WHILE (DATEPART(dw, @dt) <> 2)
	begin
	SET @dt = @dt + 1
	end

-- Return set
SELECT @dt

Open in new window

0
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 40224091
Try this:
=DateAdd("d", 1 - WeekDay (Cstr(Year(Today)) + "/04/07", 2), Cstr(Year(Today)) + "/04/07")

Open in new window

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40224123
Hi,

You can try this one

=DATEADD("ww",DATEDIFF("ww", DATEADD("d",0,"1900-01-01"), DATEADD("d", 6 - DATEPART("d", Parameters!DTS.Value), Parameters!DTS.Value)),"1900-01-01")

Here you can use the Parameter DTS value as per the date for which month you want first monday
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 500 total points
ID: 40224770
You can use a simple date calculation to get the first Monday (or given day) of a month -- no looping and definitely no I/O needed.

I think you can use a SELECT to provide a value for SSRS.

Change the "MONTH, 3" to get a different month.  For example, to get first Monday of Jan, use "MONTH, 0", for Jun, "MONTH, 5", etc..


SELECT DATEADD(DAY, -DATEDIFF(DAY, 0, month_day_7) % 7, month_day_7)
FROM (
    SELECT DATEADD(DAY, 6, DATEADD(MONTH, 3, DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0))) AS month_day_7
) AS month_day_7
0
 

Author Closing Comment

by:deborahhowson00
ID: 40226077
Cheers, worked a treat!
0
 
LVL 65

Expert Comment

by:Jim Horn
ID: 40226459
deborahhowson00

Did you try the first solutions provided you?  If yes, please comment on whether they worked.  

Not to take anything away from Scott's answer, which I'm kind of liking btw, but it is not good EE etiquette to ignor multiple earlier comments.

Just curious.
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 40228028
Perhaps.  But I think the overall quality of the answer should matter more than going strictly by the order in which answers were posted.

FWIW, I went back and checked both the "=DATEADD" codes -- neither was fully accurate.

As to the looping code, I would never consider looping if a simple SELECT could do the task flexibly and efficiently.  And it's not accurate unless the DATEFIRST setting is a specific value.

The SELECT code works regardless of any date setting.  And it can easily be completely generalized to work for any year, month and weekday.  For example, for the first Friday in June of the prior year:


DECLARE @day_of_week int --0=Mon,1=Tue,...,6=Sun.
DECLARE @month int --1=Jan,2=Feb,...,12=Dec.
DECLARE @year_adjustment int --0=current yr; -1=prior yr; 1=next yr; etc.
SET @day_of_week = 4
SET @month = 6
SET @year_adjustment = -1

SELECT DATEADD(DAY, -DATEDIFF(DAY, @day_of_week, month_day_7) % 7, month_day_7)
 FROM (
     SELECT DATEADD(DAY, 6, DATEADD(MONTH, (@month - 1), DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) + @year_adjustment, 0))) AS month_day_7
 ) AS month_day_7
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Count with a subquery showing details 10 49
Whats wrong in this query - Select * from tableA,tableA 11 45
MS SQL BCP Extra Lines Between Records 2 24
tempdb log keep growing 7 33
A recent question popped up and the discussion heated up regarding updating a COMMENTS (TXT) field in a table using SSRS. http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/MS-SQL_Reporting/Q_27475269.html?cid=1572#a37227028 (htt…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

838 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