SSIS - Calculate an interval of dates with datetime fields - datetime

Let's assume that I have some dates like these:
2014-01-23 14:52 (today)
2014-01-22-15:35
2014-01-21 10:35
2014-01-20 09:45
2014-01-19 17:58
2014-01-18 14:05
2014-01-17 13:22
Now I need to take into account only the 5 previous days to the current day, so for me they will be:
2014-01-22-15:35
2014-01-21 10:35
2014-01-20 09:45
2014-01-19 17:58
2014-01-18 14:05
In SSIS I wrote the next instruction in a conditional split task:
date > DATEADD("DD",-5,GETDATE()) && date < DATEADD("DD",-1,GETDATE())
But the result that I am having depends of the HOUR in which I execute the work flow.
So, for instance, if I execute it TODAY (2014-01-23) at 13:42. I am not going to seeing 2014-01-22-15:35 because it is after a complete day consider the hour (13:42)
and what I need is to see all the data that have a date at any moment from yesterday.
My question is, how can I indicate that I need ALL the dates of the previous days from today at 00:00?. In other words, how can I compute this interval for ALL the hours of the previous 5 days without taking into consideration the hour of execution.

if you are doing this in SQL you can try to cast the dateTime to a date
Declare #today date = getdate()
Declare #now datetime = getdate()
Cast(#now as date) between dateadd("DD",-5,#today) AND dateadd("DD",-1,#today)
let me know if that helps?

Related

How to separate column containing dates and times by a space in R

I have two columns, one of start dates and times and one of end dates and times. I want to split those into four columns: Start_date, Start_time, End_date, End_time. They are separated by a space (each column is formatted such as "12/04/2017 05:25 PM"). Ultimately I need to find the difference between the start date and time and the end date and time. I am a beginner at R so I really appropriate your help.
For the purposes of this question I am assuming that you are in the United States and thus that the example date which you provided refers to December 4th, 2017.
The first step is for you to convert the two date columns into dates instead of strings. The pattern of the elements in the datetime object must be echoed in the formatting command. Based on the example you provided, I have created a toy dataframe.
df <- data.frame(Start = c("12/04/2017 05:25 PM","05/05/2017 06:25 PM"), End = c("12/09/2018 05:15 PM","05/05/2019 06:24 PM"))
df
Start End
1 12/04/2017 05:25 PM 12/09/2018 05:15 PM
2 05/05/2017 06:25 PM 05/05/2019 06:24 PM
Now to convert these strings into date objects:
library(lubridate)
df$Start <- strptime(df$Start,format="%m/%d/%Y %I:%M %p")
df$End <- strptime(df$End,format="%m/%d/%Y %I:%M %p")
df
Start End
1 2017-12-04 17:25:00 2018-12-09 17:15:00
2 2017-05-05 18:25:00 2019-05-05 18:24:00
You will note that the spaces you indicated are included in the format pattern, along with symbols which indicate which parts of the date appear where as well as whether solo digits are padded with zeros (as all of yours appear to be). For a reference on which symbols/patterns to use in which situation, I recommend this page: https://www.stat.berkeley.edu/~s133/dates.html
If you wish to determine the difference between the two datetimes, it is now a simple matter of subtracting one from the other.
df$diff <- df$End - df$Start
Start End diff
1 2017-12-04 17:25:00 2018-12-09 17:15:00 369.9931 days
2 2017-05-05 18:25:00 2019-05-05 18:24:00 729.9993 days
In your question you asked about splitting up into pieces. Just in case that is still something that you need to do, creating the date time is still going to help you out. Now that we have datetime objects instead of strings, we can easily split the column into pieces.
df$Start_Day <- day(df$Start)
df$Start_Month<- month(df$Start)
df$Start_Year <- year(df$Start)
and so on.

Expression in SSIS - Derived column to change the Date from the last SIX days

I am working on SSIS and my question is I am having an XML file data in that data one column header is TIMESTAMP and the TIMESTAMP sample data is here:
2013-08-02 00:15:00
2013-08-03 00:30:00
2013-08-04 00:45:00
2013-08-05 08:45:00
2013-08-06 08:45:01
2013-08-08 08:45:02
In the above data, I have to change the Date to:
2017-06-02 00:15:00
2017-06-03 00:30:00
2017-06-04 00:45:00
2017-06-05 08:45:00
2017-06-06 08:45:01
2017-06-08 08:45:02
So, How can I write an Expression in the Derived Column to change the Date from last six days? Without Changing the 'TimeReading' that is side to the Date.
That means the Date should change and pop up from previous 6 days and TimeReading next to the Date should be same as previous data.
What I did:
In the Expression, if I give GETDATE() output is populating with today's date and the TimeReading is populating with 00:00:00.
If I give GETDATE()-1, output is populating with the previous date and TimeReading is populating with 00:00:00,
But I need to get the continuous date from the last six days with the same TimeReading.
Can anyone help?
I believe what you want to look at is the DateAddFunction. I think you are asking to subtract 6 days from the date in TimeReading (not sure because your example shows 2 days or 1 day). But regardless, for 6 days:
DateAdd(Day,6,TimeReading)
Create a new field using DerivedColumn to contain this new value.

Solr: Day of week, second of day with timezones

We require to search by day of week and time of day using SOLR.
That`s why we first convert date to UTC and then create two index fields: UTC second of day and UTC day of week.
We know what timezone user requests, so we are able to shift this required values and create query:
Monday from 4:00 to 6:00 AM in +5 timezone
is converted to search values for UTC:
(Sunday from 23:00 to 24:00) OR Monday (0:00 to 1:00)
Unfortunatelly this query will fail in timezones with daylight saving time, when timezone is shifted +5 is +6.
Is there a way how to query by correct time of day?
Only solution we could think of is add to query also date ranges, which were in summer/winter time:
((Sunday from 23:00 to 24:00) OR Monday (0:00 to 1:00)) AND (30.10.2015 TO 27.3.2016) OR Monday (0:00 to 2:00)) AND (27.3.2015 TO 30.10.2015 ) OR ... OR ... ,
where these ... represents date, when was switched from summer to winter time.
Data may be recorded in any timezone and viewed in any timezone as well

How does date difference work in SQL Server 2012

I am running following query in SSMS. The result of this query does not make any sense.
Question : What logic is exactly followed by SQL Server in determining this difference as it relates to this query?
SELECT
CAST('1/1/1900 11:00:00 PM' AS DATETIME) AS Date1,
CAST('1/1/1900 7:00:00 AM' AS DATETIME) AS Date2,
CAST('1/1/1900 7:00:00 AM' AS DATETIME) - CAST('1/1/1900 11:00:00 PM' AS DATETIME)
AS Date2MinusDate1
Result of this query
The thing is that internally datetime data is stored as 8 byte consisting of 2 integers: one that represents days after 19000101 and other representing ticks after midnight. So actually you are substracting 2 integers here:
Day of DT1 - Day of DT2
Ticks of DT1 - Ticks of DT2
Days are 0s since 19000101 is saved internally as 0. So first substraction gives you 0, i.e. 19000101.
The difference of ticks will give you 16 hours, but pay attension, it will be ticks(16 hours) from midnight, so actually you should substract 16 hours from midnight not 7am. Thus 00:00 - 16 hours is equal to 08:00. Of course here you shoulb decrement the result of substruction of day, i.e. 0-1=18991231.
So you end up with 18991231 08:00 and this is correct output.
But you should be very careful when using operators for date manipulation because there are nuances. Use native functions for such manipulation, like DATEADD. You can read this article, especially from page 2 http://www.devx.com/dbzone/Article/34594/0/page/2
The 12/31/1899 is SQLs equivalent to a zero date.
This is a holdover from bad date compatible software.
See this answer: Why is 1899-12-30 the zero date in Access / SQL Server instead of 12/31?
I would recommend DATEDIFF for finding differences.
SELECT
CAST('1/1/1900 11:00:00 PM' AS DATETIME) AS Date1,
CAST('1/1/1900 7:00:00 AM' AS DATETIME) AS Date2,
DATEDIFF ( hh , CAST('1/1/1900 7:00:00 AM' AS DATETIME) , CAST('1/1/1900 11:00:00 PM' AS DATETIME)) AS Date2MinusDate1

Unix -- Time zone issue

I need to predict the next run of a daily job. For example, if my job runs everyday at 1:00:00 am, and my sysdate is Nov 7 11:20:00 am, then my next run will be at Nov 8 1:00:00 am
The below logic helps me to achieve the desired results. However, when i convert the date after adding no. days to the start date, unix considers the daylight saving and converts from PDT timezone to PST timezone and gives me a time lag of 1 hour. Instead of getting Nov 8 1:00:00 am, I am getting Nov 8 00:00:00 am as output.
I want to derive date without the day light savings. Can someone help me achieve this logic in UNIX?
CURR_DATE=`date "+%s"`
START_DATE=`date -d "19OCT2014 01:00:00" "+%s"` #start date when the job was run first
diff_quo=$(((($CURR_DATE - $START_DATE) / (86400))))
diff_quo=$(($diff_quo * 86400))
new_datetime=$(($START_DATE + $diff_quo + 86400 ))
DERIVED_DATE=`date -d"#$new_datetime" "+%b %d %H:%M:%S"`
Thanks,
Ekata

Resources