Activerecord removes all zeros from the timestamp field - ruby

I am using Ruby on rails. I am trying to retrieve timestamp column from particular table.
Activerecord returns me the date, if timestamp contains all zeros.
e.g: If timestamp stored is 2010-09-06 00:00:00:000, it returns me 2010-09-06.
But if I have 2010-09-06 00:00:20:000, it returns me in the expected format(i.e: 2010-09-06 00:00:20:000).
This leads to inconsistency. Please help me improve on this.
All suggestions are welcome.
Thanks in Advance.
Sachin

What is the type of the returned object? If that's String, you may manually add missing parts, but if that is a Time object, then you may use a .strftime() method:
t + " 00:00:00.0000" if t =~ /^[-0-9]{10}$/
t.strftime("%Y-%m-%d %H:%M:%S.") + t.usec.to_s

Related

How can I create a measure that returns the newest status (non-numerical) in the data set?

I have a table in Power Pivot called tabStatus (see picture). I want to create a measure for this table that returns the status_code (a text field) for the newest status (= highest/newest time stamp in the status_change column).
I feel this should be really easy (I am new to DAX). Any ideas as to how I can create this measure?
Appreciate all the help!
I'm going to assume that your Status_change column is a valid date/time format in your locale.
Newest Status:=CALCULATE(LASTNONBLANK(tabStatus[Status_code],0),filter(tabStatus,max(tabStatus[Status_change])))

only show events who are greater than or equal laravel

Im working in a project where I display different events in my view. The goal for now is to only show events that are upcoming, so it doesn't show events that are older than today.
The users of my page can create an event and set a sertain date for it.
The data is stored into my db like this:
$table->string('datum');
In the view it return it like this:
06.03.2019
My controller to return the data looks like this:
$date = date('d.m.Y');
$evententries = Event::where('datum', '>=', $date)->orderBy('datum', 'asc')->take(3)->get();
but it's somehow not working..
Anyone got any Ideas of how to fix this and what my issue is here?
btw, I'm using laravel 5.7.
Thank you :)
Hmm you are doing it on wrong way from begin...
First: why you are using string to store date value? there is much better options to store date within database...
Second: you are using date format which cannot be compared on that way (using < > =)
when you are comparing strings ie 08.03.2019 will be always smaller than 10.01.2016 because 1 > 0... so if you are comparing dates use:
db format for storing dates
for comparing use format yyyy-mm-dd because on that way even simple string comparison will give you correct result...

Need help in Hive on date functions

I am doing functionality testing(need to write hive code while referring Scala code) in my project. I am having an issue with my date functions in my code. In Scala we have casted our date data type into string as changed its structure into ‘YYYYMM’, MY value inside my date column is like 201706(YYYYMM), which is not accepted in Hive (read that it accepts only YYYY-MM-DD).
My question is
1) How to change the YYYYMM to YYYY-MM-DD? I have tried casting to date and also UNIX_TIMESTAMP neither of them are working query is getting failed at the end.
2) We are also using filter.to_date (colm1,”YYYYMM”).between(add_months(to_date((colm2),”YYYYMM”),-27), add_months(to_date((colm2),”YYYYMM”),-2))) in our Scala code , How can I change that to HIVE? Unable to get any ideas
Thanks In advance…..
Regards,
M Sontosh Aditya
use
unix_timestamp(DATE_COLUMN, string pattern)
Further understanding please refer DateFuncitos

Comparing ruby DateTime with sqlite Date

I am attempting to pull tweets from twitter and store them in an sqlite db. In order to prevent duplication I wish to ignore tweets where the user AND the time created already exist in the DB. So my code is
if Tweet.exists?(['twitter_created_at = ? AND from_user_id_str = ?', (tweet_results.created_at), tweet_results.from_user_id_str])
Now, through taking out the user_id parts i can tell that the problem is with the comparison between
twitter_created_at
tweet_results.created_at
I have been in the debugger and seen that the dates do match but i cannot get the statement to come back true.
thanks in advance.

how to convert time field into string using linq sql?

i am using linq to fetch the data and converting time field into string.
At that time if i have a value 11:00:00 in time field of sql server.
But i am getting "Jan 1 1900 11:00AM" at the time of converting to string.
Please provide me help to fetch only time part.
Thanks in advance..
Try this:
yourDateTimeObject.ToString("HH:mm:ss")
Or as Mark Seemann suggested:
yourDateTimeObject.TimeOfDay.ToString()
It sounds like the field is of type DateTime. You can use the TimeOfDay property to get only the time part.
timeField.TimeOfDay

Resources