Sum of smalldatetime and format as decimal minutes SQL - 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

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'));

SQL how i can convert from datetime to total hours?

I do this query and the result is a datetime, i try so much variants, but nothing work it... I want the result to be displayed in the number of total hours, like (in this case): 25:01:05 because i have 2 days en this datetime, I have had results like 01:01:05 which is when it only subtracts the hours from the datetime. I would like that as well as add the hours by the number of days, do it with the months if it can be
The time datatype in SQL Server only holds up to 24 hours.
I would recommend decimal hours instead:
select datediff(second, 0, calchstrabajos) / (60.0 * 60)
Note: I switched from millisecond to second because that is usually sufficient.
If you want this in the form of HH:MM:SS, then you would need to convert to a string. I don't recommend that.

sqlite converting milliseconds to HH:MM:SS.SSS

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

SQL-Server Time data type

From what i've read so far, the data type time in sql server (starting with 2008) should be able to store the time in a HH:MM format. Then I tried this myself with the following simple exercise:
create table #mytable (id int, lat float, lon float, trajectory_id int, theTime time(5))
insert into #mytable values
('1','15.8','17.1','162','10:01'),
('2','11.5','59.7','162','10:02'),
('3','16.4','79.9','162','10:03'),
('4','29.5','10.3','180','11:12'),
('5','58.2','11.1','180','11:13'),
('6','54.5','14.1','180','11:14'),
('7','14.9','15.2','166','13:40'),
('8','15.0','13.1','166','13:42')
My expected result for the column theTime was:
theTime
10:01
10:02
10:03
11:12
11:13
11:14
13:40
13:42
What I got instead was:
theTime
10:01:00.00000
10:02:00.00000
10:03:00.00000
11:12:00.00000
11:13:00.00000
11:14:00.00000
13:40:00.00000
13:42:00.00000
Sure, I could select left(theTime, 5) and get the expected result. But i'm wondering how I could insert it directly in the desired format.
I want to use a built in function and not to store the numbers in an int for HH, MM, SS as one might have used before sql server 2005.
Using sql server 2012. Thanks
You seem confused by the difference between internal formats and display formats. TIME is a data type built into the language. When it prints out, it prints out with hours, minutes, and seconds. That is how the data type prints out, in the same way the SQL Server does not put commas in integers bigger than 1000 and uses an appropriate symbol for the decimal place. (Or for that matter prints out integers in base 10 rather than the binary format as they are actually stored.)
When you say TIME(5) you are saying that you want at least 5 decimal places of precision for fractional seconds: HH:MM:SS.SSSSS. There is no option for removing seconds altogether from the time type.
If you want to print it out in a particular format, use convert(), cast(), or format(). Happily, cast() works quite well for times. The plethora of inscrutable numeric codes for date/time are more applicable to dates, than times.
When you do left(thetime, 5) then you are implicitly converting time to a string and taking the first five characters. This seems to meet your needs. I would suggest that you just build this into your table:
alter table #mytable add thetime_hhmm as (left(thetime, 5));
Then you can use thetime_hhmm to get the value as a string with the format you want.
You can't control the format that a data type is stored as. Your time data is actually being stored as a bunch of bits -- 0's and 1's -- and it's only being displayed like 10:01:00.00000 in your query analyzer.
If you want to show it in another format, then you have to convert it to a text data type like varchar, but this is usually something your UI layer should handle, not your data layer.