I want to only pull results that correspond to todays date. I am new to Redshift but am familiar with MYSQL. The closest I can get is 24 hours but I would prefer to use only data from today, I have been using:
history_date >= date(GETDATE())- Interval '24 hours'
Would using the current date function be another alternative? Here is is entire query in case any one is interested :)
You should be able to use the DATE_TRUNC function to get the date with the time portion removed.
history_date >= date(DATE_TRUNC('day', GETDATE()))
Related
I want to subtract two date in impala. I know there is a datediff funciton in impala but if there is two timestamp value how to deal with it, like consider this situation:
select to_date('2022-01-01 15-05-53','yyyy-mm-dd HH24-mi-ss')-to_date('2022-01-01 15-04-53','yyyy-mm-dd HH24-mi-ss') from dual;
There is 1 minute difference and oracle would put the result as 0.000694444 days.
My requirement is if there is any such functionality in impala where I can subtract two timestamp value in the manner 'yyyy-mm-dd HH24-mi-ss', and get the result in equivalent days irrespective of if there is difference in days , year, hours, minute or seconds. Any difference should reflect in equivalent number of days.
Any other way where I can achieve the same thing, I am open to that as well.
Thank you in advance.
You can use unix_timestamp(timestamp) to convert both fields to unixtime (int) format. This is actually seconds from 1970-01-01 and very suitable to calculate date time differences in seconds. Once you have seconds from 1970-01-01, you can easily minus them both to know the differences.
Your sql should be like this -
select
unix_timestamp(to_timestamp('2022-01-01 15-06-53','yyyy-MM-dd HH-mm-ss')) -
unix_timestamp(to_timestamp('2022-01-01 15-05-53','yyyy-MM-dd HH-mm-ss')
) diff_in_seconds
Once youhave difference in seconds, you can easily convert them to minutes/hours/days - whatever format you want it.
I need to get db results where the question answered_at (date) is itself plus some days, from a column. I've tried a lot using Carbon and also mysql INTERVAL but I get stuck on the syntax.
I'm using Laravel 6 and Carbon.
I have a table called "questions" and from there I would like to get the id of a question that was answered_at a certain date plus an amount of days that I have in another column called "repeat".
Something like this:
->where( 'answered_at', '<', Carbon::now()->subDays('repeat'))
However, I can't just write "repeat", as it doesn't recognize it as a table. If I write a number in there it actually works.
This works:
->where( 'answered_at', '<', Carbon::now()->subDays(10))
But since the dynamic amount of days to add to a date comes from the database, I must have it as a dynamic field. I have tried to get it before the query, and use it as a $variable, however - that creates two different mysql-queries and would mean bad performance.
So I would like this to work, however I can't figure out the syntax:
->where( 'answered_at', '<', Carbon::now()->subDays('COLUMN_REPEAT_FROM_DB'))
I have also tried this:
->whereRaw("DATE_ADD(answered_at, INTERVAL 10 DAY) <= NOW()")
which works but INTERVAL 10 DAY is then hard coded and
->whereRaw("DATE_ADD(answered_at, INTERVAL 'repeat' DAY) <= NOW()")
doesn't work either..syntax!
The entire query looks like this:
$question = \DB::table('questions')
->where( 'answered_at', '<', Carbon::now()->subDays('repeat'))
->value('answered_at');
(I'm getting answered_at as value just to see which date comes out in order to test it, will change to id later and pass on to another function).
Any pointers highly appreciated!
You were on the right way with your last query, but you used wrong quotes.
You should use backquotes instead, in order to specify column name:
$query->whereRaw("DATE_ADD(answered_at, INTERVAL `repeat` DAY) <= NOW()")
Alternatively, you can use this query with the same result:
$query->whereRaw("DATEDIFF(NOW(), answered_at) >= `repeat`")
Though, the calculcation itself seems wrong. If you want to select rows which have current date within answered_at + repeat DAYS interval then it should be >= in your query and <= in mine.
I'm trying to get the hour from a timestamp column, i use the HOUR() funtion and im available to get it but i need to get the 2 digit hour.
This is what i already try:
HOUR("Column.Name")
This is what i get in my output, do not worry about the minutes:
For example in my output i have 7:00 and 9:30, after using the HOUR function i would like to get 07 and 09 instead of 7 and 9.
Anyone can help me?
select LPAD(extract(hour from SYSTIMESTAMP),2,'0') from dual;
You can use the Oracle extract functionality which extracts hour from a TIMESTAMP field.
Note : It does not work with DATE datatype only TIMESTAMP should be an input parameter
Oracle Reference : https://docs.oracle.com/cd/B28359_01/server.111/b28286/functions052.htm#SQLRF00639
I have read many answers for combining date and time and nothing worked so far. I am working in Oracle SQL developer version 3.1.06 and I am trying to combine date and time stamps together. Date is in format dd-mmm-yy. And time is in the following 3 formats-
1. 0348A-- meaning 3:48 am
2. 03:48:00
3. 228 -- meaning minutes from midnight, calculated as (3*60)+48.
And for all these timestamps, I want a query that gets me to this format --
mm/dd/yyyy hh:mm:ss .
I can change the dates and times to string and attach them, but then when I work in powerpivot I am not able to change them to the required format. So, I want to do it in the query itself.
I have already tried something like this-
1. CAST(deptdt as DATETIME)+CAST(time as DATETIME)
2. CAST(depdt AS TIMESTAMP(0)) + (depdt - TIME '00:00:00' HOUR TO SECOND) AS DATETIME
Please help!!
I would like to get the records that have their created_at date closest to the current date. How can I do this with active records' where clause?
You could find the closest record in the past with something like:
Record.where("created_at <= ?", Date.today).order_by("created_at DESC").limit(1)
Similarly, you can have the closest record in the future
Record.where("created_at >= ?", Date.today).order_by("created_at ASC").limit(1)
And then compare wich one is the closest to current date...
There may be a solution to do it with a single request, but I could not find how (if you're using SQL server, there's a method DATEDIFF that could help).
Update: Thanks to Mischa
If you're sure that all created_atare in the past, you're looking to the last created record, that could be written
Record.order("created_at").last
Update
To get all the records created the same date then the last record:
last_record_date = Record.max(:created_at)
Record.where(:created_at => (last_record_date.at_beginning_of_day)..(last_record_date.end_of_day))