sqlite converting milliseconds to HH:MM:SS.SSS - sql

I have a column in the table that stores milliseconds as an Integer type in sqlite. The millisecond timestamp is stores the time it took for certain event happen. For example, it took 36 milliseconds for Action 1 occurred, the value 36 will be stored into the table.
I want to construct a SQL select statement that will format the millisecond timestamp to HH:MM:SS.SSS format, and so far I couldn't get it working.
Here's what the SQL query that I constructed.
select strftime('%H:%M:%f', table_foo.time/1000, 'unixepoch') as time from table_foo
The query result returned 00:00:00.000 where I was expecting to see 00:00:00.036 when the timestamp is 36 ms.
It appears that it is not showing the reminder properly.
Can someone points me what I did wrong? or what is the appropriate way to do what I need?
Thanks in advance.

try the following, replace int 1000 with decimal 1000.0.
select
strftime('%H:%M:%f', table_foo.time/1000.0, 'unixepoch') as time
from table_foo

Related

How to get time from Date & time field in SQL? [duplicate]

In my table I have a datetime field that stores dates in this format:
YYYY/mm/dd HH:MM:SS
Now I need to retrieve via query only the time.
I have tried this
SELECT time(start_date)
FROM table
LIMIT 100
but no luck, it gives me "not an error" but no records return
Any idea?
EDIT
I solved it! The problem is that SQLite needs Times to be in a format in which if hours, minutes and seconds are less than 10 they must be represented with the zero.
For example:
H:M:S
WRONG --> 12:1:30
RIGHT --> 12:01:30
Moreover, the correct format for dates is YYYY-mm-dd and not YYYY/mm/dd.
There is a built-in function in SQLite called strftime(format,datetime) which can be used to get whatever piece of information you require from the given datetime. In your case you can use in this way:
SELECT strftime('%H:%M:%S',start_date) FROM table LIMIT 100;

Calculate time difference in SQL

We have two columns in SQL. one is total_work_time & next is total_exeption_time & both column data type is varchar
total_work_time value is 07:15:00
total_exeption_time value is 01:15:00
So I need to subtract total_work_time - total_exeption_time and the result will be 06:00:00.
I have tried with concat(DATEDIFF(HOUR,total_exeption_time,total_work_time),':', DATEDIFF(MINUTE,total_exeption_time,total_work_time))
But the result is 6:360. from this, 360 is the problem, it taken total minutes. I need the result structure like 06:00:00. How to fix this issue using SQL Server.
You should be storing time values in a TIME datatype - using the correct datatype is not only a best practice but will reduce the problems you face in future.
You can convert your VARCHAR values to TIME and then use the following calculation which takes the difference in seconds (your lowest unit of interest one assumes) and creates a new TIME result.
DECLARE #total_work_time TIME = '07:15:00', #total_exeption_time TIME = '01:15:00';
SELECT CONVERT(TIME, DATEADD(SECOND, DATEDIFF(SECOND, #total_exeption_time, #total_work_time), '00:00'));

Subtracting two times to get duration

I am trying to subtract departure time from arrival time to get the duration of trips.
The arrival/departure is in the format of HH:MM.
I'm using postgresql, the time columns just say 'string' for how they are defined. A friend helped me cast them as integers, but I'm still stick at this point:
SELECT x, cast (table1.arrival as integer) - cast (table.1departure as integer), sum(y), count(*)
FROM table1 INNER JOIN table2
ON .....
GROUP BY .....
ORDER BY....
A friend told me this but I'm not totally sure what he means: "No you need to create a new variable for just minutes. 1:30 = 90 minutes. Cast that as integer then compute." How do I do this?
If the column is indeed a varchar (or text) then you need to first cast the column to a proper time.
SELECT x, table1.arrival::time - table.1departure::time, sum(y), count(*)
FROM table1 INNER JOIN table2
ON .....
GROUP BY .....
ORDER BY.
The above will fail if the time stored in the columns is not a properly formatted time in 24 hour format (e.g. 22:40)
The result of time - time is an interval. If you want to get the result in minutes, you need to extract them from the interval:
extract(epoch from table1.arrival::time - table.1departure::time) / 60
extract(epoch ..) returns the seconds in the interval that's why you need to divide it by 60 to get minutes.
This will however not work properly if the arrival and departure time are not on the same day (e.g. departure is at 23:25 and arrival is the next day at 07:23)
Unrelated but: you should never store dates, times or timestamps in varchar or text columns. Always use the proper data type.
If your RDBMS is Mysql you can use the MySQL DATEDIFF() Function
For example :
SELECT DATEDIFF(arrivale,departure) AS DiffDate
I assume here that the arrivale and the departure are in the same day.
And that arrivale and departue are TimeDate formated like HH:MM.

Sum of smalldatetime and format as decimal minutes SQL

Working with an Access database, using SQL commands to pull data from it. I have a column 'Duration' formatted in smalldatetime, which contains data pertaining to the duration of a phone call. The data is this column is like so
0/01/1900 12:00:26 AM
This indicates a call that is 26 seconds in duration. (all start at 12:00:00 AM and work upwards)
I have a column called 'Extension' which identifies the user of the phone.
I am wanting to run a query that will show me total durations, but grouped by extensions.
The query I have so far is
SELECT (CONVERT(VARCHAR(8),(DATEADD(ms, SUM(DATEDIFF(ms, '00:00:00.000', (CONVERT(VARCHAR(8),Duration,108)))), '00:00:00.000')),108)) as "timetest",
Extension AS Extension
FROM BrendanTest GROUP BY Extension
This shows:
Timetest Extension
00:12:00 117
00:06:00 118
which is correct.
However, I want to format the result, to show in decimal minutes, for example
00:04:56
would become
4.93
Is there a way I can do this? Thanks a lot for the help!
Assuming '00:04:56' is in the format HH:MM:SS, you can try something like the below to convert it to decimal format:
SELECT PARSENAME(REPLACE('00:04:56',':','.'),3) * 60 --Convert the hours to minutes
+ PARSENAME(REPLACE('00:04:56',':','.'),2) --Add converted hours to minutes
+ CONVERT(FLOAT, LEFT((CONVERT(INT,(PARSENAME(REPLACE('00:04:56',':','.'),1)))/0.6),2))/100 -- convert to decimal format

Add milliseconds to date in SQL, not seconds

I'm writing a procedure on the SAP HANA database (using SPS 07).
In this procedure I save the current timestamp in a variable:
vMyTimestamp := CURRENT_UTCTIMESTAMP;
I know from HANA's SQL and System Views Reference that there are currently 5 functions available to add an amount of time to a date:
ADD_YEARS
ADD_MONTHS
ADD_DAYS
ADD_WORKDAYS
ADD_SECONDS
However, what I need is to add only a certain amount of milliseconds to a date, so for example if the current timestamp is something like 2014-04-10 09:11:45.0 I want to get 2014-04-10 09:11:45.1
Is there a way to achieve this?
Thanks in advance.
You should be able to add the seconds as a fractional value (e.g., 0.1 seconds = 100 milliseconds). The following, adds 100 milliseconds to the given timestamp:
SELECT ADD_SECONDS (TO_TIMESTAMP('2014-04-10 09:11:45'), 0.1) FROM DUMMY