How to pass a value from the Sqlite DB to julianday() [duplicate] - sql

This question already has answers here:
Difference in seconds between timestamps in Sqlite3
(2 answers)
Closed 2 years ago.
I need to calculate the difference between two dates represented as TEXT. julianday() can do this, but it accepts a literal:
select(julianday('2015-01-01 12:00:00') - julianday('2015-01-01 13:00:00'))
But if I have a table:
CREATE TABLE "time_u" (
"time_in" TEXT,
"time_out" TEXT
);
INSERT INTO time_u (time_in, time_out)
VALUES ('2007-01-01 10:00:50', '2007-01-01 11:00:00')
how can I do something like? :
julianday(time_u.time_out-time_u.time_in)
in other words take values directly from the database and subtract them as a date

You can turn each date to Julian days, then substract:
select
t.*,
julianday(time_out) -julianday(time_in) day_diff
from time_u t
If you want something more accurate, you can turn the datetimes to epoch timestamp. Say you want the difference in minutes, then:
select
t.*,
(strftime('%s', time_out) - strftime('%s', time_in)) / 60.0 day_diff
from time_u t

Related

How do i get the full hour instead of the minutes? [duplicate]

This question already has answers here:
Truncate date to only hour / minute
(4 answers)
T-SQL datetime rounded to nearest minute and nearest hours with using functions
(4 answers)
Group DateTime into 5,15,30 and 60 minute intervals
(6 answers)
Closed 6 months ago.
I'm tring to extract hour values from a datetime:
I want all the datetimes like '2021-10-27 04:55:00.000' to show the hour data, so: '2021-10-27 04:00:00.000'
What query do i run to get this?
Thanks in advance!
Use date maths and a "magic" date:
DATEADD(HOUR,DATEDIFF(HOUR,0,YourColumn),0);
This gets the number of hours between the "date" 0 (1900-01-01) and your date value, and then adds that many hours to the "date" 0.
On SQL Server 2022 (currently in preview), however, you have access to DATETRUNC and DATE_BUCKET that make this much easier:
DATETRUNC(HOUR,YourColumn),
DATE_BUCKET(HOUR,0,YourColumn)
The current time (UK) is 11:06 (am) and:
select format(getdate(), 'yyyy-MM-dd HH')
..returns 2022-08-12 11
You can add whatever you want on the end of it, e.g.:
select format(getdate(), 'yyyy-MM-dd HH') + ':00:00'
..which gives 2022-08-12 11:00:00
NB Format is a SQL-Server function. I'm not sure how many other databases have it.
For the string representation of a date and time value you can do something like this
SELECT CONCAT(FORMAT(dt, 'yyyy-MM-dd HH'),'00:00.000')
see Custom formatting and Custom Date and Time Formatting
use convert
declare #a datetime='2021-10-27 04:55:00.000'
select convert(varchar(10),#a,120) + ' '+convert(varchar(2), datepart(hour,#a))+':00:00'

How to convert date time in varchar to date in oracle to get one month data [duplicate]

This question already has answers here:
Get the records of last month in SQL
(2 answers)
Closed 12 months ago.
I have data in oracle table where date field Created_Date is in format 01/01/2022 7:00:00 PM which is of type varchar2 ,i want to get past one month data, and i did below query which is not working
select *from Mn_Fdd_tbl where to_date(to_char(Created_Date,'DD/MM/YYYY'), 'DD/MM/YYYY' ) > trunc(sysdate)-30;
Should be
select *
from mn_fdd_tbl
where to_date(created_date, 'dd/mm/yyyy hh:mi:ss pm') > add_months(trunc(sysdate), -1);
because
no point in TO_CHAR-ing something that's already a string ...
... with a wrong format model
"last month": not all months have 30 days, so - your "calculation" is wrong for approx. 50% of months. Use add_months instead

SQL difference between two datetime columns

I have a dataset with 2 columns of datetime datatype as shown here:
I want to take the difference between the two dates and I try it with this code:
Select
*,
original_due_date - due_date as difference
from
Table
However I'm not sure if the same would suffice as this is a datetime and not just date.
Any inputs would be much appreciated.
Desired output
The question was originally tagged Postgres, so this answers the original question.
Presumably, you are storing the values as timestamps. If you just want the results in days, then convert to dates and take the difference:
Select t.*,
(t.original_due_date::date - t.due_date::date) AS difference
from Table t;
If you want fractional days, then a pretty simple method is to extract the "epoch", which is measured in seconds, and use arithmetic:
Select t.*,
( extract(epoch from t.original_due_date -
extract(epoch from t.due_date
) / (24.0 * 60 * 60) AS decimal_days
from Table t;
transform timestamps to seconds (unix_timestamp), calculate difference and divide by (60*60*24) to get days
select (unix_timestamp(original_due_date, 'MM-dd-yyyy HH:mm')-unix_timestamp(due_date, 'MM-dd-yyyy HH:mm'))/(60*60*24) as difference_days
from (select '07-01-2021 00:00' as due_date, '02-10-2020 00:00' as original_due_date) t
Result:
-507

how to convert mysql TIMESTAMPDIFF to oraclesql query [duplicate]

This question already has answers here:
Calculating difference between two timestamps in Oracle in milliseconds
(11 answers)
Calculate difference between 2 date / times in Oracle SQL
(21 answers)
Closed 3 years ago.
TIMESTAMPDIFF(HOUR, pk.consprtydate, rs.consprtydate)
wanted the difference of hours from two dates and extract the hours in oracle sql
In oracle, you can easily achieve it by substracting two date fields as following:
Date2 - Date1 -- returns number of days between dates
(Date2 - Date1)/24 -- returns number of days between dates
MONTHS_BETWEEN(Date1, Date2) -- returns number of months between dates
Cheers!!
It depends on the datatype. IF they are of Oracle type TIMESTAMP, then follow the methods already answered here
If they are of type date, then - as said by Tejash- simple subtraction gives the difference in days that you would then need to round to hours. (integer portion will be days = 24 hours, and the decimal portion will be in fractions of a day)
e.g.) to round down to the integer number of hours without a fractional part:
(days * 24 hours, plus the fraction of a day * 86400 seconds in a day / 3600 seconds in an hour)
SELECT trunc(date1 - date2) * 24 + trunc((mod (date1-date2,1) * 86400) / 3600)
FROM DUAL

Postgresql sql query convert time in second [duplicate]

This question already has an answer here:
Convert timestamp column values to epoch in PostgreSQL select query
(1 answer)
Closed 3 years ago.
I have MySQL DB and i am using following syntax to convert my datetime type in second
select UNIX_TIMESTAMP(created_at) as time_sec, blah..blah..
But now i have other postgresql DB which has timestamp with time zone type so how do i convert that time in second with my select query?
2018-04-18 18:27:48.96283+00 This is this format of timestamp in table which i want to convert in seconds
you can use
select extract(epoch from created_at) as time_sec from tsepok
db<>fiddle here
more docs here