In the file we are getting time field as 1300,1400,1500...0000 in that I need to take first two digits and subtract with 1
example
time:1300 means exact time is 12
1 to 23 we can take it by using Substr(time,1,2)-1
In the case of 0000 hour how can we do that ?
0000 is the same as 24 hrs? Then
'case when substr(time,1,2)='00' then 23 else substr(time,1,2)-1 end'
Related
I have a column 'Duration' it holds the time the therapist spent with the client.
This is always entered as minutes so if the time was 3 hours it is entered as 180. I would like to set this in the query as 3.
This is how it is reporting from a canned report: Total duration time is the entered column, it is
defined as int,null. I would like to make this calculation and formatiing, in the sql for the shown column 'total duration'.
total_duration_num total_duration
10 0:10
120 2:00
30 0:30
5 0:05
60 1:00
One means of achieving this is to use:
the floor function to round down when dividing the minutes by 60 (for whole hours)
the mod function to get the remaining number of minutes after putting however many can fit into "whole" 60-minute hours
the lpad function to put a leading zero before that number of minutes, if <10, so that you see :05 rather than :5 for example
The query would look like this:
select duration,
concat( floor(duration/60) , ':' , lpad(mod(duration,60),2,'0') ) as hrs_mins
from duration_table;
This is a demonstration:
http://sqlfiddle.com/#!9/a52b6c/1/0
I've a PSQL table like this:
Order
Start_Hour
Start_Minute
Finish_Hour
Finish_Minute
10
10
15
12
15
10
12
15
14
15
10
16
00
17
00
And I need to calculate by a query the total time expressed in hours that I spent to finish the order. In this scenario I expect to have a total of 5 hours:
12:15 - 10:15 = 2 hours
14:15 - 12:15 = 2 hours
17:00 - 16:00 = 1 hours
The query result must be 5.
The idea was concatenate start hour/minute and finish hour/minute, convert them to hour, make the difference, calculating the total.
SELECT (Start_Hour & ":" & Start_Minute) as start, (Finish_Hour & ":" & Finish_Minute) as finish
FROM OrderDetails
But when I try to convert them to HH:MM using cast or convert but I got errors.
Any advice?
Thank you
This query uses make_time as Adrian Klaver suggests.
select
"Order",
sum(extract(hour from
make_time("Finish_Hour", "Finish_Minute", 0) -
make_time("Start_Hour", "Start_Minute", 0))
) as duration
from the_table
group by "Order";
However I have remarks about your data design. Hour and minute are not enough for storing time because (apart from missing precision and other reasons) the end time might be over midnight. You have a specific data type for this - timestamp. I would suggest something like
create table the_table
(
order_nr integer,
start_time timestamp,
finish_time timestamp
);
Also note that using mixed case names in Postgresql requires double-quoting.
Use make_time:
select make_time(12, 15, 0) - make_time(10, 15, 0);
?column?
----------
02:00:00
Where in your case you would substitute in Start_Hour, Start_Minute, Finish_Hour, Finish_Minute.
I am using T-SQL and am looking to find some values using the 'simplest' query possible. By this I mean I would like to be able to do this logic in only one statement / where clause if possible.
A user has a baseline date (let's say 25th November 2016), and every 90 days (+/-14 days) a 'time window' opens that allows them to access some data. In this case it would be between the dates 9th Feb 2017 to 9th March 2017. This is an ongoing arrangement, and consequently 90 days (+/-14 days) after 23 Feb 2017 the same thing happens again.
I need to be able to calculate if a user is within any of these windows from only knowing the baseline date.
I was initially thinking of using DATEDIFF(DAY, baseline_date, GETDATE()) % 90 = 0 but I realised that won't allow me to account for the +/- 14 days.
Any ideas?
You want the days -14 - 0 (before the zero) and 0 - 14 (after the zero).
The % 90 will give values from 0 to 89 (no negatives). But because it cycles and resets at 90, the -14 - 0 is the same as 76 - 89.
So you want days 0 - 14 and 76 - 89:
where datediff(day, baseline_date, GETDATE()) % 90 not between 15 and 75
I am using Now.Hour to return an integer for a query. Here is my sql query:
SELECT * FROM DATABASE.dbo.STORE_LIVE WHERE DELIVERY_HOUR=Now.Hour
However, at 12 midnight, my query is not returning any row since Now.Hour only returns '0' but the DELIVERY_HOUR in the database has a range of 1 to 24.
In MSDN Time Format, these are the only available formats:
"H"
The hour, using a 24-hour clock from 0 to 23.
6/15/2009 1:45:30 AM -> 1
6/15/2009 1:45:30 PM -> 13
"HH"
The hour, using a 24-hour clock from 00 to 23.
6/15/2009 1:45:30 AM -> 01
6/15/2009 1:45:30 PM -> 13
How should I customized Now.Hour to return from 1 to 24 without resorting to a conditional statement for interval 0 or 23?
Thanks
I would suggest you create an extension method which returns 1 - 24 instead of 0 - 23.
Put this code into a module:
Imports System.Runtime.CompilerServices
<Extension()>
Private Function GetCustomHour(aDate As Date) As Integer
Dim hour = aDate.Hour
Select Case hour
Case 0 : Return 24
Case Else : Return hour
End Select
End Function
Then in your code:
Instead of Now.Hour use Now.GetCustomHour
However, I would strongly advise that you change your database to store valid times as this is only going to cause more problems along the way (there is no such hour of the day as 24)
I am adding hours to the datetime object in sybase.
dateadd(hh,4,date_val)
The problem is that if the current hour is 2am, after adding 4 hours it is displaying 6 instead of 06. How do i achieve the appending of 0 if the time is in single digit? Please help.