Just the day formating in SSIS expression builder - date

I'm trying to format(in yyyymmdd) the previous month's first day within the SSIS 2005 expression builder. So far I have managed to get the year and month correctly represented. However, I'm stuck when it comes to the day part of the expression.
Below is the complete errorneous code which I'm trying to hack.
RIGHT((DT_WSTR, 4) YEAR( DATEADD( "mm",-1, getdate() )), 4) +
RIGHT("0" + (DT_WSTR,2) MONTH( DATEADD( "mm",-1, getdate() )), 2)+
RIGHT("0" + (DT_WSTR,2) DAY(DATEADD("mm", DATEDIFF("mm", 0, GETDATE())-1,0)),2)
The first line returns the year ( in yyyy format) the second line returns the month (mm format). However, I'm stuck at returning the day part (in format 01 or just 1) in the third line.
Any help is appreciated.

There's no need to do any fancy expression stuff for the day. The first day of the month is always 01 so your expression should be what you already had for year and month plus the concatenation of the literal string "01"
RIGHT((DT_WSTR, 4) YEAR( DATEADD( "mm",-1, getdate() )), 4) +
RIGHT("0" + (DT_WSTR,2) MONTH( DATEADD( "mm",-1, getdate() )), 2)+
"01"

This will return a 2 char day part for any date:
right('0'+cast(day(getdate()) as Varchar(3)),2)

Related

Expression in SSIS returning date that does not exist

I have an expression that returns the date 6 months ago in the format 03/31/2021. This has been functioning as desired until today when it returned the date 09/31/2020, which does not exist. How can write the expression to get the desired results?
RIGHT("0" + (DT_WSTR,2)MONTH( DATEADD( "MONTH" , -6,GETDATE())), 2) +
"/"+ RIGHT("0" + (DT_WSTR,2)DAY(GETDATE()), 2) +
"/" +(DT_WSTR,4)YEAR(DATEADD( "MONTH" , -6,GETDATE()))
The problem is the second set of string logic
RIGHT("0" + (DT_WSTR,2)DAY(GETDATE()), 2)
It is getting the day number of current day (31). You likely intended to back that off 6 months as you do with Year and Month.
RIGHT("0" + (DT_WSTR,2)DAY(DATEADD("MONTH" , -6,GETDATE()), 2)
Were it me, I would create a new variable, AnchorDate and set Evaluate as Expression to True and use the expression
DATEADD("MONTH", -6, GETDATE())
I can test that, yes it's 6 months ago.
Now my date string becomes cleaner as I am not repeating (or not in 1/3 of your case) the date manipulation logic.
RIGHT("0" + (DT_WSTR,2)MONTH(#[User::AnchorDate]), 2) +
"/" + RIGHT("0" + (DT_WSTR,2)DAY (#[User::AnchorDate]), 2) +
"/" +(DT_WSTR,4)YEAR(#[User::AnchorDate])

Design a query for SSRS report to switch between hour, day and month time period grouping

I have this code that works as I want.
SELECT
DATEADD(
DAY,
DATEDIFF(
DAY,
'19000101',
fdEnterDate
),
'19000101'
) AS theHour,
COUNT (*) AS theCount
FROM
DB.dbo.[table]
WHERE
fdEnterDate >= '11/01/2014'
AND fdEnterDate < '11/11/2014'
GROUP BY
DATEADD(
DAY,
DATEDIFF(
DAY,
'19000101',
fdEnterDate
),
'19000101'
)
ORDER BY
theHour
I want to give the user a drop down to pick between hour, day and month
The code would change the four instances of 'DAY' in the query to what ever the user selected in the drop down.
I'm not sure how to approach this. I'd rather not create a new report for each selection.
theHour theCount
2014-11-03 00:00:00.000 1390
2014-11-04 00:00:00.000 2027
2014-11-05 00:00:00.000 2442
2014-11-06 00:00:00.000 1284
2014-11-07 00:00:00.000 3296
2014-11-08 00:00:00.000 842
2014-11-10 00:00:00.000 1407
these are the results of the 'day' query so the hour would break down the results by the hour in between the dates specified. That I know how to do but I need to know how to integrate that into an ssrs report by allowing the user to select the hour or day or month from a dropo down.
Almost everything in Reporting Services is an expression, including the Sql query statement. Right-click the dataset, click Dataset Properties and click the expression editor beside the query box, which is the fx button. Now we can edit the query as a string expression.
Let's say you have a parameter called Period with the selectable values HOUR, DAY, and MONTH. We change your Sql expression to this:
="SELECT "
&" DATEADD(" & Parameters!Period.Value & ", DATEDIFF(" & Parameters!Period.Value & ", '19000101', fdEnterDate), '19000101') AS theHour, "
&" COUNT (*) AS theCount "
&"FROM DB.dbo.[table] "
&"WHERE fdEnterDate >= '11/01/2014' AND fdEnterDate < '11/11/2014' "
&"GROUP BY DATEADD(" & Parameters!Period.Value & ", DATEDIFF(" & Parameters!Period.Value & ", '19000101', fdEnterDate), '19000101') "
&"ORDER BY theHour "
Apart from line formatting, this is exactly the same query you had, but converted to a string expression with the word DAY replaced by Parameters!Period.Value. When the report runs it evaluates the string expression inserting the period selected by the parameter and runs the result as a Sql query. Want to add WEEK or YEAR as period selections? Add them to the parameter's available values and the query will do that as well.

SSRS expression, iif statement with date conditions

So my default values for startDate and endDate in SSRS were set up with the following ssrs expressions.
first day of previous month ssrs expression
=DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
last day of previous month ssrs expression
=DateAdd(DateInterval.Day, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1))
But that won't work alone in my case unless I want to go in on the 16th of every month and generate this report for the people requesting it for the first 15 days of the current month.
So in my default value expression for start date i am trying this iif statement...
= iif(
DatePart(DateInterval.Day, Today() <> "20",
DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1),
DateInterval.Month, 1, DateSerial(Year(Date.Now), Month(Date.Now), 1)
)
Not working out so well. So what i'm trying to do is.....
Change the default start and end date based on what day of the current month it is, So if current day of the current month equals 16, make start date 1 of current month and end date 15 of current month, if current day of the month isn’t 16 make start date first of previous month and end date last day of previous month. So then the only thing needed is to get subscription emails and what day to send them out on.
Untested, but what if you try this? (for your start date parameter):
= iif(
DatePart(DateInterval.Day, Today()) <> "16",
DateAdd(DateInterval.Month, -1, DateSerial(Year(Date.Now), Month(Date.Now), 1)),
DateSerial(Year(Date.Now), Month(Date.Now), 1)
)

How do I format a date in a SQL Where clause?

I have a .NET 2010 app that bangs against a SQL db. On the app side, a user can search on Begin date and End Date. Bot of these are just Month + Year. I then format them so they are complete dates. So when they go to the stored proc they'll look like this...
Begin Date: 1/1/2011
End Date: 5/31/2011
But the date in the db is broken up into 3 int fields, Month,Day & Year, ...of which Day may or may not be filled in (0 if not). It would be ok for this to always default to one when running this query. So if the values in the db were Month=3, Day=0 Year=2011 I would like the sql statement to render as
Where FORMATTEDDATEHERE between '1/1/2011' and '5/31/2011'
I just can't figure out how to format sql fields in a where clause.
Did you try something like
WHERE CONVERT(DATETIME,
CONVERT(VARCHAR(4), [c_year]) + '/'
+ CONVERT(VARCHAR(2), [c_month]) + '/'
+ CONVERT(VARCHAR(2), [c_day]))
BETWEEN '2011/1/1' AND '2011/5/31'
Hope it helps
you could build the date using datadd functions within the WHERE clause EG.
SELECT ...
WHERE DATEADD(Day, field_days-1, DATEADD(Month, field_months-1, DATEADD(Year, field_years-1900, 0))) BETWEEN '1/1/2011' AND '5/31/2011'
Just replace field_days, field_months, field_years with the int fields on the table for days, months and year. This will return all records within the date range.
Is this what you require?
WHERE CAST(Year AS varchar) + RIGHT(100 + Month, 2) +
RIGHT(100 + COALESCE(NULLIF(Day, 0), 1), 2) BETWEEN ...

Calculate the number of weeks by date

I am trying to fix a function which returns the number of weeks in a given year.
Here's how it looks:
Function GetWeekNo(date)
weekOfYear = DatePart("ww", DateValue(date), vbMonday, vbFirstFourDays)
If weekOfYear > 52 Then
If DatePart("ww", DateValue(date) + 7, vbMonday, vbFirstFourDays) = 2 Then
weekOfYear = 1
End If
End If
GetWeekNo = weekOfYear
End Function
When this function is given the date 12-31-2010 it returns 52. There are 53 weeks in 2010.
Note: I have no experience with classic ASP, what-so-ever.
Seems like it depends on which week is considered as the "first week of the year".
DatePart( "ww", "12/31/2010", vbMonday )
' returns 53
' FirstWeekOfYear parameter defaults to vbFirstJan1
' the week that contains January/01/2010
' here, its the week starting on December/28/2009
DatePart( "ww", "12/31/2010", vbMonday, vbFirstFourDays )
' returns 52
' FirstWeekOfYear parameter set to vbFirstFourDays
' the first week that has at least four days of the new year
' here, its the week starting on January/04/2010
DatePart( "ww", "12/31/2010", vbMonday, vbFirstFullWeek )
' returns 52
' FirstWeekOfYear parameter set to vbFirstFullWeek
' the first week that has full seven days of the new year
' here, again, its the week starting on January/04/2010
Decide what is your definition of the first week of the year, then use the DatePart function accordingly.