SSIS how to compare time field - time

I get a time column which is in DT_DBTime type. (e.g.05:00:21.0000000). Now I need to compare this time column with specific time slot. However, it shows errors when I write in this way:
[Time]>="05:00:00.0000000"&&[Time]<="05:01:00.0000000"
Is there any way I could compare time values without converting to string type?

You should be able to cast your string and compare that way:
[Time] >= (DT_DBTime)"05:00:00.0000000" && [Time] <= (DT_DBTime)"05:01:00.0000000"
If that doesn't work, trying using CONVERT:
[Time] >= CONVERT(DATETIME,"05:00:00.0000000") && [Time] <= CONVERT(DATETIME,"05:01:00.0000000")

Related

golang Compare time size

I want to make a comparison between the current time and the database data. My code is:
var PayBlockedLog[] model.PayBlockedLog
global.DB.Where("status = ?",0).Find(&PayBlockedLog)
for i := 0; i <len(PayBlockedLog); i++ {
if time.Now().Unix() < PayBlockedLog[i].UnsealTime{
}
}
It seems that I should have PayBlockedLog[i].UnsealTime convert to int64.
After trying, there was no effect
Rather than fetching and then filtering, do the filter in the query. This will save time and memory, as well as avoid time zone issues.
I don't know what database you're using, nor the column type of UnsealTime. If it's a timestamp or datetime (which it should be) check it against current_timestamp.
global
.DB
.Where("status = ? and current_timestamp < UnsealTime", 0)
.Find(&PayBlockedLog)
If it's seconds since the Unix epoch, consider converting it to a normal timestamp column so date/time functions can be used. There are several ways to accomplish this while keeping the old column for compatibility.
Otherwise, you can convert the current time to epoch seconds. How you do this depends on the database. Here it is in Postgres.
global
.DB
.Where("status = ? and extract(epoch from current_timestamp) < UnsealTime", 0)
.Find(&PayBlockedLog)

How to convert dates within an array to a specific format

I have an array of dates, where the dates have different formats. I would like to convert them all to the same format, however what I've tried has caused me some problems.
I get the dates from a SQL query:
table_birth_dates = self.class.connection("SELECT birth_date FROM #{temp_table_name}").values
which gives me an array of dates:
[
[0] "10/3/80",
[1] "10/3/81",
[2] "10/3/01",
[3] "33/33/1970",
]
I want to get the year as a full year like above.
I tried:
table_birth_dates.first.to_time
and got:
ArgumentError: argument out of range
I also tried:
Date.strptime(table_birth_dates)
and got:
no implicit conversion of Array into String
Does anyone have any ideas?
As the others already said: Don't store arbitrary strings as date! Use your database and store dates as date or datetime. See documentation of your database. (See comment from #the-tin-man.)
That said you can try the following with your current date:
table.map! { |date|
date_hash = Date._strptime(date, '%m/%d/%Y') # {:mon=>10, :mday=>3, :year=>70}
if date_hash.nil?
'invalid'
else
date_hash[:year] = date_hash[:year] + 1900 if date_hash[:year] < 1000
Date.new(date_hash[:year], date_hash[:mon], date_hash[:mday])
end
}
This gives you an array with actual dates like so:
[
1980-10-03
1981-10-03
1901-10-03
invalid
]
Of course you should do something about this invalid but it serves your main purpose.
If all you dates is on the the same format as the first 3 dates you can use this con change the format of the dates:
dates.map! do |date|
Date.strptime(date, '%d/%m/%y').strftime('%d/%m/%Y')
end
If you have dates on different formats you could list the formats you have and try to parse each of them and see if you get a valid result, however dates like 33/33/1970 is not a valid date at all.
Parsing dates isn't as straightforward as people assume when they first start working with them, because dates are represented differently around the world.
'10/3/80'
would represent "October 3, 1980" in a U.S. formatted date, but would be "March 10, 1980" in the rest of the world. And, the software has no idea which it is unless you tell it which to use. And, if your data comes from around the world you can't tell unless you know what locale the data was generated in or unless they explicitly tell you what format it's in.
require 'date'
Date.strptime('10/3/80', '%m/%d/%y') # => #<Date: 1980-10-03 ((2444516j,0s,0n),+0s,2299161j)>
Date.strptime('10/3/80', '%d/%m/%y') # => #<Date: 1980-03-10 ((2444309j,0s,0n),+0s,2299161j)>
From DB, bring date in integer format and convert back to any specific format.
Are you storing date in DB as string and trying to convert them for unique format to display? If so, you can correct while storing the value (or) add a column in DB which gets updated whenever your date column gets inserted/updated and retrieve from new column.

Epoch time difference in Pig

I have 3 columns which contains start_time , end_time and tags. Times are represented in epoch time format as shown in example below. I want to find the the rows which have 1 hour time difference between them.
Example:
Start_time End_Time Tags
1235000081 1235000501 "Answered"
1235000081 1235000551 "Answered"
I need to fetch the tags column if the time diff is less than an hour.
I want do it in PIG - can anyone kindly help?
input.txt
1235000081 1235000501 Answered
1235000081 1235000551 Answered
pig script
A = Load '/home/kishore/input.txt' as (col1:long, col2:long, col3:chararray);
B = Foreach A generate ToDate(col1) as startdate,ToDate(col2) as enddate,col3;
C = Filter B by GetHour(enddate)-GetHour(startdate) == 1;
Dump C;
you can filter the row based on your condition like >,< ,==
In case if you want to keep date fields as timestamps the solution is following:
data = LOAD '/path/to/your/input' as (Start_Time:long, End_Time:long, Tags:chararray);
data_proc = FOREACH data GENERATE *, ToDate(Start_Time*1000) as Start_Time,ToDate(End_Time*1000) as End_Time;
output = FILTER data_proc BY GetHour(End_Time)-GetHour(Start_Time) == 1;
Dump #;
The one crucial thing is that Pig ToDate UDF needs a timestamp up to milliseconds precision thus you will have simply multiply your date fields by 1000 before using this UDF.

How do I get the aggr of two aggrs in QlikView?

If I want to find the maximum value of a column from two states aggregated by a member's ID, should this work?
=Aggr(
MaxString(
Aggr(NODISTINCT MinString({[State1]}DATE_STRING),MBR_ID)
+
Aggr(NODISTINCT MinString({[State2]}DATE_STRING),MBR_ID)
) , MBR_ID)
So if I had this data:
MBR ID DATE_STRING
1 20120101
1 20120102
1 20120103
And State1 had 20120101 selected and State2 has 20120103 selected, my expression would return 20120103 for member 1.
Thanks!
Edit: In SQL, this would look like:
WITH MinInfo (DATE_STRING, MBR_ID)
AS (SELECT MIN(DATE_STRING), MBR_ID FROM Table WHERE TYPE IN ('State1', 'State2') GROUP BY MBR_ID, TYPE)
SELECT MAX(DATE_STRING) DATE_STRING, MBR_ID FROM MinInfo GROUP BY MBR_ID
It would be easier to accomplish your goal if you convert your that to an actual date field
Assuming that you are using a chart where MBR_ID is the Dimension, if you want the maximum date (latest date) you can do the following:
=nummax(Max({[State1]}DATE_STRING),Max({[State2]}DATE_STRING))
To convert to a date, you can use this function:
date#(DATE_STRING,'[text format of the date]')
(The date format looks like YYYYMMDD to me, but if its day then month, you would use YYYYDDMM)
I'd suggest you format it in the script, so that you wont have to worry about it every time you need to use that date.

DateTime Comparison in LINQ

I am ran into the following issue.
Take a look at the following where clause.
Where(x => x.CreateTS <= dateParameters.CreationDate.ToDateValue &&
x.CreateTS >= dateParameters.CreationDate.FromDateValue)
CreateTS is a Timestamp column in the table. so when my dateparameters are todate = 01/28/2010 12:00A.M" fromdate= "01/26/2010 12.00A.M" (c# DateTime types) my query doesnot retrivies the table records whose CreateTS look like 01/28/2010 1:45A.M and above just differeing in the timestamps.
I just wanted to do comparison and dont want to compare the timestamps. any help would be appreciated.
Just look at the .Date portion of the DateTime:
Where(x => x.CreateTS.Date <= dateParameters.CreationDate.ToDateValue && x.CreateTS.Date >= dateParameters.CreationDate.FromDateValue)
As a side note:
Personally, I would put from first, since it's a bit easier to follow from a readability standpoint:
Where(x => x.CreateTS.Date >= dateParameters.CreationDate.FromDateValue && x.CreateTS.Date <= dateParameters.CreationDate.ToDateValue)
(I find it easier to think of being between two values as being >low and
Edit: You didn't specify that you were using the EF LINQ providers, which do not allow you to do Date. You can, I believe, handle this by looking forward one day, and using < instead of <= for your "to" comparison:
Where(x => x.CreateTS >= dateParameters.CreationDate.FromDateValue && x.CreateTS.Date < dateParameters.CreationDate.AddDays(1).ToDateValue)
#SARAVAN, AFAIK timestamp columns cannot be used in comparisons like that. A timestamp columns are mapped as byte[] (byte array) in EF. If comparisons like that are needed, I suggest to change the timestamp column in database to a datetime one.
only to quote from MSDN:
The SQL Server timestamp data type has nothing to do with times or dates. SQL Server timestamps are binary numbers that indicate the relative sequence in which data modifications took place in a database.
Interesting moment here is that
01/28/2010 12:00A.M actually in 24 hour format is 00:00 while 01/28/2010 1:45A.M is 01:45 and that seems to be the reason why those records are not returned. The midday will be 01/28/2010 12:00P.M

Resources