How to use oracle TIMESTAMP with fractional seconds in NHibernate? - oracle

I have this mapping in my NHibernate.
LASTUPDATE is a TIMESTAMP row, which stores fractional seconds
However, when I have query like
q => q.Where(p => p.LastUpdate > _lastupdate);
where _lastupdate is a DateTime (with fractional seconds, of course) , NHibernate translate _lastupdate as '2012-03-12 8:48:25', losing the fractional seconds.
How can I fix this situation?

You have to explicitly map your property as type="Timestamp"
See 5.2.2. Basic value types

Related

NEST (2.x) Date Histogram Aggregation with fractional interval values

I am using NEST (2.3.3) object initializer syntax for creating Date Histogram Aggregation. How can I specify the Fractional values for the Interval?
DateHistogramAggregation dateHistogram =
new DateHistogramAggregation("dateHistogram")
{
Field = "TimestampFieldName",
Interval = DateInterval.Hour
}
In the above data histogram aggregation I want to specify for example 1.5 hours. Is there a way I can do that?
Interval is a Union<DateInterval, Time> which means that it can take either a DateInterval enum value or a Time instance. Additionally, a string has an implicit conversion to an instance of Time. Putting these together, to set an interval of 1.5 hours would be
DateHistogramAggregation dateHistogram =
new DateHistogramAggregation("dateHistogram")
{
Field = "TimestampFieldName",
Interval = new Time("1.5h")
};
In this case, we can't take advantage of the implicit conversion from string to Time (and then Time to Union<DateInterval,Time>) because there is no implicit conversion from string to Union<DateInterval, Time>. In this case, we can just use the Time constructor and pass it a string value for 1.5 hours, and assign this instance of Time to the interval.

Ruby on Rails 3 convert time to DateTime in specific timezone

I have column which stores only time. I want to convert to it into datetime and then to a specific timezone. When I do it on console using only the attribute, it shows correct conversion value. But when I use it in query the query shows the current time in UTC. Suppose I have time "05:15" stored in Central time. Now when I want to fetch records for a interval of 30 minutes plus and minus of this time, I do following,
day = Time.now
# departure_time_from _db below is the name of column in table which store time
departure = departure_time_from _db.change(:day => date.day, :month => date.month, :year => date.year)
departure = departure.in_time_zone("Central Time (US & Canada)")
When I execute above code on console and see the value, it shows correct converted value. But when I use this variable in below query, current time gets used instead of the departure time.
Model.where("column_name > ? and "column_name < ?, departure - 30.minutes, departure + 30.minutes)
The time used in above query should be the time I want i.e Central time zone, but in query output, it shows UTC being used.
Please let me know what i am missing.
It shouldn't matter what time zone the query uses in the where clause as long as it's the same time representation. The time you're seeing in your query output from Rails is most likely the UTC equivalent of the ruby DateTime object being passed to the where statement. So if departure is 12:00 noon (UTC -6) in your Ruby code then the time shown in the SQL query will be 18:00, which corresponds to noon CST.

How can I group on the created_at field and return only the Date and hour

I run the following in Rails:
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'DATE(created_at)')
which returns a hash with the items nicely grouped.
However I want to group not by date only by date but by the hour, but not the minutes or seconds.
How can I run this query so all the heavy lifting is done by Postgresql and not convert after I get the data set back?
My mistake I want to group by Date and Hour. Sorry for the confusion.
It sounds like you want the date_trunc() function:
http://www.postgresql.org/docs/current/interactive/functions-datetime.html#FUNCTIONS-DATETIME-TABLE
I want to group not by date but by the hour, but not the minutes or
seconds.
Bold emphasis mine.
So date_trunc()is probably not what you want, but EXTRACT or date_part():
coe = trackers.where(:trackable_type => "Mailing").count(:group => 'EXTRACT (hour FROM created_at)')

Oracle DateAdd() of 2 different columns

I'm trying to figure out a DateAdd() equivalent in Oracle that is actually the difference in seconds between 2 columns in the same table:
SELECT
DISTINCT p.packet_id,
p.launch_dt,
r.route_duration,
s.completion_date,
DATEADD(SS, r.route_duration, p.launch_dt) AS tempDate
FROM
tdc_arc_apprpkt_def p
JOIN tdc_arc_inpr_route_def r
ON p.packet_id = r.packet_id
JOIN tdc_arc_inpr_route_step_detai s
ON p.packet_id = s.packet_id
AND s.completion_date > DATEADD(SS, r.route_duration, p.launch_dt)
Any help would be greatly appreciated!
In addition to being able to do date arithmetic using fractions of days as Tony demonstrates, assuming you are using 9i or later, you can also use interval functions (or, even better, define the ROUTE_DURATION column as an interval) and add intervals to dates. In your case, you can do
p.launch_dt + numtodsinterval( r.route_duration, 'SECOND' )
to add route_duration seconds to launch_dt.
If you were to define the route_duration column as an INTERVAL DAY TO SECOND rather than a NUMBER, you could simply add it to a date
p.launch_dt + r.route_duration
If I understand you correctly, you want to add r.route_duration seconds to p.launch_dt? In that case the expression is:
p.launch_dt + (r.route_duration/24/60/60)
Oracle DATE arithmetic works in days, so the divisions by 24, 60 and 60 convert the route_duration value from seconds to days.

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