I am trying to figure out how to get the last day of the month given with a SSIS Expression.
I receive the column as an Integer, then added a Data Conversion step to convert the YYYMM to a date format.
Then on derived column, my expression is:
REPLACE((DT_WSTR,10)(DT_DBDATE)DATEADD("D",-(DAY(DATEADD("M",1,[Copy of MONTH]))),DATEADD("M",1,[Copy of MONTH])),"-","")
And on final table I am getting the value 24520504.
Example: I am getting the value for the derived column from an .xls file.
On that file, I use the column MONTH and it is an Integer (example: 201711).
On the final table it is an Integer too, but it has the last day from 201711 (example: 20171131)
EDIT: After your help, I reformulate my expression but I still get an error:
REPLACE((DT_STR,10,1252)DATEADD("d",-1,DATEADD("m",1,(DT_DBDATE)(DT_STR,6,1252)[MONTH])),"-","")
Assuming that this column is being input to the Derived column as a DT_DBDATE date type, the following expression will get the last day of the month. This expression essentially parses out the year and month first, uses these to build a new date by adding one month to this date (now in the next month), and then subtracts one day, which is the last DATEADD functions role, and then returns the date back to the same month, but now on the last day. Finally, the outer DAY function returns the day.
DAY(DATEADD("day",-1,DATEADD("month",1,(DT_DBDATE)((DT_STR,4,1252)YEAR((DT_DBDATE)YourDateColumn) + "-" + RIGHT((DT_STR,2,1252)MONTH((DT_DBDATE)YourDateColumn),2) + "-01"))))
Update:
For the DT_R8 data type that stores the date using the YYYYDD format, use the following expression to return the last day of the month.
DAY(DATEADD("day",-1,DATEADD("month",1,(DT_DBDATE)SUBSTRING((DT_STR,12,1252)LEFT((DT_STR,8,1252)YourDateColumn,4) + "-" + SUBSTRING((DT_STR,8,1252)YourDateColumn,5,2) + "-01",1,12))))
where are you starting (this will return last date of month in date format)?
in SQL:
declare #YYYYMM as int = 201811
select dateadd(d,-1,dateadd(m,1,cast(cast(#YYYYMM as varchar(6))+'01' as date)))
This will take it all the way to int:
declare #YYYYMM as int = 201811
select cast(replace(cast(dateadd(d,-1,dateadd(m,1,cast(cast(#YYYYMM as varchar(6))+'01' as date))) as varchar(10)),'-','') as int)
In SSIS:
it's similar. I don't have a way to confirm it right now though. I'll edit later.
this should work...
periodo ='201803'
DATEADD("day",-1,DATEADD("month",1,(DT_DBDATE)((DT_STR,4,1252)LEFT(period,4) + "-" + RIGHT(period,2) + "-01")))
Related
I'm trying to make 3 separate fields from a date and now I ran into this problem. When the (DDMMYYYY European style) date is like 04032017, the code:
SELECT SUBSTRING(CAST(04032017 AS VARCHAR(38)), 2, 2) AS Month
returns 03 (perfect).
But when the code is like (the first zero is now a 1!) :
SELECT SUBSTRING(CAST(18022017 AS VARCHAR(38)), 2, 2) AS Month
the result is 80 because SUBSTRING now is counting from the (1) first position and in the first example it took 4 as the starting point.
Obviously I need to have 1 code for all occurrences but I just don't get it right.
Some help would be appreciated!
Regards, J
This should work for you:
select SUBSTRING(right('00000000' + CAST(04032017 AS varchar(38)),8), 3, 2) as Month
You might require to parse this as varchar and then convert into valid date and use month() function to get clear approach
declare #date nvarchar(10)='18022017'
select Month(cast(CONCAT(SUBSTRING(#date,3,2),'/',SUBSTRING(#date,1,2),'/',SUBSTRING(#date,5,4)) as date))
Ok, just solved it. Just remove the cast (why did I use that in the first place?) and put single quotes around the date:
select SUBSTRING('04032017'), 2, 2) as Month
This works just fine!
Variable: report_date_time
11/8/2005 12:00:00 AM
How to destring this variable into a date variable with Stata?
I do not need the time (12:00:00 AM) and need to keep the dates only (11/8/2005).
The starting point is reading help datetime and the linked-to section help datetime translation. Assuming your sample date is month-day-year order (and not day-month-year), the following example demonstrates the key concepts. The use of "#" in the mask argument to daily() (which is a more expressive name for the date() function) tells it to ignore the stuff after the year; without that a missing value is the result.
clear
input str30 date_s
"11/8/2005 12:00:00 AM"
end
generate newdate = daily(date_s,"MDY#")
format newdate %td
list, clean
And the output is
date_s newdate
1. 11/8/2005 12:00:00 AM 08nov2005
I am trying to create a table within a report that appears as follows:
The data set is based on this query:
SELECT
DATENAME(dw, CurrentReadTime) AS 'DAY',
DATEPART(dw, CurrentReadTime) AS 'DOW',
CAST(datename(HH, CurrentReadTime) as int) AS 'HOD',
AVG([Difference]) AS 'AVG'
FROM
Consumption
INNER JOIN Readings ON Readings.[RadioID-Hex] = Consumption.[RadioID-Hex]
WHERE
CONCAT([Building], ' ', [Apt]) = #ServiceLocation
GROUP BY
CurrentReadTime
ORDER BY
DATEPART(DW, CurrentReadTime),
CAST(DATENAME(HH, CurrentReadTime) AS INT)
The data from this table returns as follows:
In report builder, I have added this code to the report properties:
Function GetRangeValueByHour(ByVal Hour As Integer) As String
Select Case Hour
Case 6 To 12
GetRangeValueByHour = "Morning"
Case 12 to 17
GetRangeValueByHour = "Afternoon"
Case 17 to 22
GetRangeValueByHour = "Evening"
Case Else
GetRangeValueByHour = "Overnight"
End Select
Return GetRangeValueByHour
End Function
And this code to the "row group":
=Code.GetRangeValueByHour(Fields!HOD.Value)
When I execute the report, selecting the parameter for the target service location, I get this result:
As you will notice, the "Time of Day" is displaying the first result that meets the CASE expression in the Report Properties code; however, I confirmed that ALL "HOD" (stored as an integer) are being grouped together by doing a SUM on this result.
Furthermore, the actual table values (.05, .08, etc) are only returning the results for the HOD that first meets the requirements of the CASE statement in the VB code.
These are the things I need resolved, but can't figure out:
Why isn't the Report Properties VB code displaying "Morning", "Afternoon", "Evening", and "Overnight" in the Time of Day column?
How do I group together the values in the table? So that the AVG would actually be the sum of each AVG for all hours within the designated range and day of week (6-12, 12-18, etc on Monday, Tuesday etc).
To those still reading, thanks for your assistance! Please let me know if you need additional information.
I'm still not sure if I have a clear picture of your table design, but I'm imagining this as a single row group that's grouped on this expression: =Code.GetRangeValueByHour(Fields!HOD.Value). Based on this design and the dataset above, here's how I would solve your two questions:
Use the grouping expression for the value of the Time of Day cell, like:
Add a SUM with a conditional for the values on each day of the week. Example: the expression for Sunday would be =SUM(IIF(Fields!DOW.Value = 1, Fields!AVG.Value, CDec(0))). This uses CDec(0)instead of 0 because the AVG values are decimals and SSRS will otherwise throw an aggregate of mixed data types error by interpreting 0 as an int.
I get the following exception :
conversion failed when converting date and/or time from character
string
at this statement :
select CAST(RIGHT('0' + CAST(overtimeHours as varchar(2)), 2) + ':00:00' As Time)
from #GPilot;
NOTE : this happens when overtimeHours = 24
That's because '24:00:00' is not a valid time. Use a modulo to truncate that:
SELECT CAST(RIGHT('0' + CAST((overtimeHours % 24) AS varchar(2)), 2) +
':00:00' As Time) FROM #GPilot;
There is no 24:00:00 hour. you should test if it's 24 and if so return 00:00:00 and add a day to the date part (if you have a date part), or change to 23:59:59, or simply use modulo like in Amit's answer. The correct thing to do depends on the wider context that you didn't share in your question.
Does anybody know how I can convert a date and time column to a decimal column based on the time only?
For example:
Select StaffNum, Name, ContractHours
From StaffContract
Where StaffNum = 00123
Returns
**StaffNum**--------**Name**----------------**ContractHours**
00123-----------Shaw, Jason--------2012-10-01 07:30:000
Instead of the ContractHours column showing '2012-10-01 **07:30:000**' I need it to show the time section only and convert it to a decimal so instead of 07:30 I need it to show 7.5
You can use the datepart to extract parts of the date:
SELECT StaffNum,
Name,
DATEPART(hour, ContractHours) + (DATEPART(minute, ContractHours) / 60.0)
FROM StaffContract
WHERE StaffNum = 00123