Is it possible to convert integer to days and hours in SQL? - sql

I am using SQL Server 2014.
What I'm trying to do is add a new time to an old datetime.
I'm not even sure if it's possible but I thought I'd ask the experts.
So these are what my columns look like:
CurrentDate | Hours | NewDate
2017-03-10 08:00:00 | 25 | ??
2017-01-01 10:00:00 | 27 | ??
What I want is the Hours to be converted to days and hours so it can be added to the CurrentDate to create a NewDate.
So the NewDate would be: 2017-03-11 09:00:00 because 25 hours equates to 1 day and 1 hour. And the second NewDate would be: 2017-01-02 01:00:00 because 27 equates to 1 day and 3 hours.
I actually don't think this is possible and there's a chance I might have to put the hours already converted into days and times but if that's the case, how would I write 25 hours? Would it be 00-00-01 01:00:00? And would 27 hours be 00-00-01 03:00:00 and then just add those values into CurrentDate?
Thanks! Feel free to tell me this has been asked before (I looked, but couldn't find anything as unique as this or maybe I didn't look hard enough) or if this can't be done.

You can simply use DATEADD, no need to convert the hours to days first:
SELECT CurrentDate,
Hours,
DATEADD(HOUR,Hours,CurrentDate) NewDate
FROM dbo.YourTable;

You can try this:
select DATEADD(HOUR,25,'2017-03-10 08:00:00') -- 2017-03-11 09:00:00.000
select DATEADD(HOUR,27,'2017-01-01 10:00:00') -- 2017-01-02 13:00:00.000

Related

SQL BigQuery: How to populate dates from rows cycle_base and cycle_interval

I'm having trouble populating dates with variable cycle_base (day of week) and cycle_interval (days) columns in Google BigQuery SQL.
The idea is to populate a date array for 2022 for each product where the dates fall within the valid_from and valid_to dates and where the dates are generated with the respective cycle_interval
A snippet from my data looks like this:
cycle_base
valid_from
valid_to
cycle_interval
product
2016-09-19
2020-04-20
2022-12-31
7
A
2018-12-17
2020-01-27
2022-12-31
28
B
2019-12-30
2020-01-27
2022-12-31
56
C
I tried generating a date array and then joining those dates on the DAYOFWEEK, which ofcourse only works for rows with a interval of 7 days. But I can't seem to find a way to achieve the above with the other intervals.
Edit
Expected data for 2022:
The cycle_base represents the day of the week. The exact date is irrelevant really. Edit: The date represents the starting point from which to interval the weeks.
Product A starts on monday for every week.
Product B starts on monday for every 3 weeks.
Product C starts on monday for every 8 weeks.
date
product
dayofweek
cycle_interval
2022-01-03
A
monday
7
2022-01-10
A
monday
7
...
...
...
...
2022-01-03
B
monday
28
2022-01-17
B
monday
28
...
...
...
...
2022-01-03
C
monday
56
2022-02-21
C
monday
56
...
...
...
...
Hope someone can point me in the right direction :)
Thanks in advance,
Glenn

Trying to get an MS Access query the hours in one day

I have three 5 columns in my query:
Count(ID)
Min(Date)
Max(Date)
Hours: DateDiff("h",Min([Date]),Max([Date]))
UPH: Count([ID])/[Hours]
I want the query to return the count per day, for example:
Count(ID) | Min(Date) | Max(Date) | Hours | UPH
30 | 9/29/2020 11:00:00 AM | 9/29/2020 11:00:00 PM | 12 | 2.5
I have tried the following code but had no luck
Between #9/29/2020 12:00:01 AM# And #9/29/2020 11:59:59 PM#
Seems like I can only get it to run within a day range, not an hourly range.

Reset the date portion to the last of the month while preserving the time

Is there any way to reset the date portion to the last day of the month while preserving the time? For example:
2018-01-02 23:00:00 -> 2018-01-31 23:00:00
2018-04-04 10:00:00 -> 2018-04-30 10:00:00
The Oracle function last_day() does exactly this. Try:
select last_day(sysdate), sysdate
from dual
to see how it works.
Ironically, I usually find the preservation of the date to be counterintuitive, so my usual usage is more like:
select last_day(trunc(sysdate))
from dual

Sql Select Time Stamp and Represent it as 9 Hours Back

Good day. I have a table that is collecting data in Universal Time Coordinated timestamps. However, the location is 9 hours back from this time. I am writing a query that gets the time-stamp and the value but 'casts' the timestamp 9 hours back since thats when it got recorded with respect to that location.
My issue is that I keep subtracting days not hours even though I specified hours in my 'datediff' and 'dateadd'. How do I select a timestamp and the value but represent that timestamp as 9 hours back? Thanks for any help.
select DATEADD(hour, DATEDIFF(hour,9,TimeUTC),0) as DateActual, Value
From TableData
Data
2015-12-15 00:00:00 45
2015-12-15 00:00:00 54
Current results
2015-12-06 00:00:00 45
2015-12-06 00:00:00 54
Desired results
2015-12-14 15:00:00 45
2015-12-14 15:00:00 54

How do I generate a series of hourly averages in MySQL?

I've got data in ten minutes intervals in my table:
2009-01-26 00:00:00 12
2009-01-26 00:10:00 1.1
2009-01-26 00:20:00 11
2009-01-26 00:30:00 0
2009-01-26 00:40:00 5
2009-01-26 00:50:00 3.4
2009-01-26 01:00:00 7
2009-01-26 01:10:00 7
2009-01-26 01:20:00 7.2
2009-01-26 01:30:00 3
2009-01-26 01:40:00 25
2009-01-26 01:50:00 4
2009-01-26 02:00:00 3
2009-01-26 02:10:00 4
etc.
Is it possible to formulate a single SQL-query for MySQL which will return a series of averages over each hour?
In this case it should return:
5.42
8.87
etc.
It's unclear whether you want the average to be aggregated over days or not.
If you want a different average for midnight on the 26th vs midnight on the 27th, then modify Mabwi's query thus:
SELECT AVG( value ) , thetime
FROM hourly_averages
GROUP BY DATE( thetime ), HOUR( thetime )
Note the additional DATE() in the GROUP BY clause. Without this, the query would average together all of the data from 00:00 to 00:59 without regard to the date on which it happened.
This should work:
SELECT AVG( value ) , thetime
FROM hourly_averages
GROUP BY HOUR( thetime )
Here's the result
AVG(value) thetime
5.4166666865349 2009-01-26 00:00:00
8.8666666348775 2009-01-26 01:00:00
3.5 2009-01-26 02:00:00
There is also another possibility considering the fact that dates have a string representation in the database:
You can use SUBSTRING(thetime, 1, [len]), extracting the common part of your group. For the example with hourly averages you have the SQL query
SELECT SUBSTRING(thetime, 1, 13) AS hours, AVG(value) FROM hourly_averages GROUP BY hours
By the len parameter you can specify the aggregated time interval considering the MySQL date format yyyy-MM-dd HH:mm:ss[.SS...]:
len = 4: group by years
len = 7: group by months
len = 10: group by days
len = 13: group by hours
len = 16: group by minutes
len = 19: group by seconds
We encountered a better performance of this method over using date and time function, especially when used in JOINs in MySQL 5.7. However in MySQL 8 at least for grouping both ways seem to take approximately the same time.