[Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Creating tables and queries
Post Reply
Panaiotis82
Posts: 9
Joined: Tue Mar 19, 2024 4:00 pm
Location: Albuquerque, NM, USA

[Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Panaiotis82 »

I have a database of student lesson records. The Lessons table includes a lesson start time and a length in minutes. How do I add a time field with the (start time + length)?

This is the current relevant parts of the SQL query without an attempted end time:
SELECT "Lessons"."Date",
"Lessons"."Lesson.Time" AS "Time",
"Lessons"."Length",
FROM "Lessons", "Students" WHERE "Lessons"."Date" >= CURRENT_DATE

Embedded database: HSQL database engine
Last edited by Panaiotis82 on Wed Mar 20, 2024 7:53 pm, edited 5 times in total.
OpenOffice 4.1.15
Mac OS 13.5.2
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Villeroy »

Embedded HSQL does not have a DateAdd function like most other database engines have. As a work-around, you can calculate the day fractions and add them as you would do in a spreadsheet. A formatted control on a form or report can display this value as a time.

Code: Select all

SELECT *,
Hour("Time")/24 + Minute("Time")/1440 + Hour("Length")/24 + Minute("Length")/1440 AS "DayFraction"
FROM "Lessons"
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Panaiotis82
Posts: 9
Joined: Tue Mar 19, 2024 4:00 pm
Location: Albuquerque, NM, USA

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Panaiotis82 »

After 4 hours of failed attempts to use and modify your example, I figured out a solution. First, the Length value is an integer, not a time value, so the Minute("Length") function crashed OO. However, removing the MINUTE() function always returned a value of zero until I added several zeros after a decimal point in the division value. The following example works:

The field types and names are: time: "Time", int: "Length"
The formula that works is:

SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + "Length" /1440.00000 AS "End" FROM "Lessons"

Then I change the "End" column to a Time format. This works as long as the Length value is not blank (NULL?)
OpenOffice 4.1.15
Mac OS 13.5.2
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Villeroy »

Any operation with a Null value returns Null.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Panaiotis82
Posts: 9
Joined: Tue Mar 19, 2024 4:00 pm
Location: Albuquerque, NM, USA

Re: [Solved] SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Panaiotis82 »

Yes, I see that. Thanks for your help. I've been trying for years to solve the time issue: literally. I will try something similar for dates. Hpefully I will be able to work out the details on my own.
OpenOffice 4.1.15
Mac OS 13.5.2
Panaiotis82
Posts: 9
Joined: Tue Mar 19, 2024 4:00 pm
Location: Albuquerque, NM, USA

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Panaiotis82 »

I reset this as unsolved because the solution only partially works, and has exposed anomalous and finicky behavior. While I was able to get something to work under extremely limited conditions, implementing it in the necessary or any usable context consistently fails.

The SQL editor requires the formula to be without spaces: e.g. HOURS("Time")/24.00000. However, after closing the editor and re opening it, the spaces are added back and have to be removed before saving any edits.

Worse than that, this formula doesn't work in a table form at all.

In a query, OO crashes if the query includes references to a linked table. These references work if there are no calculated fields, so I can either have the calculation or the reference fields, but not both.

There is probably some magic combination that will provide an acceptable solution, but I have been unable to find it, and even systematically trying different combinations has not gotten me any closer.
OpenOffice 4.1.15
Mac OS 13.5.2
chrisb
Posts: 298
Joined: Mon Jun 07, 2010 4:16 pm

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by chrisb »

original post deleted by author.

i had uploaded an attachment which demonstrated an alternative formula in query & form.
the attachment was never downloaded by Panaiotis82 so therefore i have withdrawn my contribution.
Last edited by chrisb on Thu Mar 21, 2024 5:43 am, edited 1 time in total.
open office 4.1.14 & LibreOffice 6.4.4.2 x64 using HSQL 1.8.0.10 (Embedded) and HSQL 2.6.0 (Split) on Windows 10
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Mountaineer »

Panaiotis82 wrote: Tue Mar 19, 2024 10:21 pm ..."
The formula that works is:
SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + "Length" /1440.00000 AS "End" FROM "Lessons"

Then I change the "End" column to a Time format. This works as long as the Length value is not blank (NULL?)
Functions IFNULL or COALESCE can provide an alternative value for NULL like

Code: Select all

SELECT "Time", "Length", HOUR( "Time" ) /24.00000 + 
COALESCE("Length" /1440.00000, 0) AS "End" FROM "Lessons"
Another way around this would be to provide a default value for the column, then you may even set it to NOT NULL.
OpenOffice 3.1 on Windows Vista
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Mountaineer »

Panaiotis82 wrote: Wed Mar 20, 2024 2:58 am ....
The SQL editor requires the formula to be without spaces: e.g. HOURS("Time")/24.00000. However, after closing the editor and re opening it, the spaces are added back and have to be removed before saving any edits.
...
Never seen that, but I'm using LibreOffice.
If you wish to keep your precise query, you may set "direct SQL"-mode. (Rightmost icon, if I'm remembering right). You can't use :param then, but OpenOffice should not change a single character in your query.
OpenOffice 3.1 on Windows Vista
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Mountaineer »

Panaiotis82 wrote: Wed Mar 20, 2024 2:58 am...
In a query, OO crashes if the query includes references to a linked table. These references work if there are no calculated fields, so I can either have the calculation or the reference fields, but not both.
...
Show the code here, so one can check, because this is not a real restriction of Base, even if you may have found a bug.

Work-arounds: Try direct SQL, if the cause is Base when it interprets your query or save a working query as View in the database (e.g. with calculated fields), then add the reference fields using the view as datasource.
OpenOffice 3.1 on Windows Vista
Panaiotis82
Posts: 9
Joined: Tue Mar 19, 2024 4:00 pm
Location: Albuquerque, NM, USA

Re: SQL Query: Need "Time" + "MinuteLen" as "EndTime"

Post by Panaiotis82 »

For some inexplicable reason OO is behaving better today. Here is the SQL line that works:

SELECT "Lessons"."Was.Taught" AS "Taught", "Students"."Name.first", "Students"."Name.last", "Lessons"."Date" AS "Date",
"Lessons"."Lesson.Time" AS "Time", "Lessons"."Length" AS "Length",
HOUR( "Lesson.Time" ) / 24.000000000 + MINUTE( "Lesson.Time" ) / 1440.000000000 + "Length" / 1440.000000000 AS "End",

"Lessons"."Assignments" AS "Assignments", "Lessons"."notes" AS "Notes" FROM "Lessons", "Students", "Date-1" WHERE "Lessons"."Student.ID" = "Students"."Student.ID" AND "Lessons"."Was.Taught" = FALSE AND "Lessons"."Date" >= "Date-1"."First" AND "Lessons"."Date" <= "Date-1"."Last" ORDER BY "Date" ASC, "Time" ASC

Notice how many zeros past the decimal places it takes to give me clean values when the result is formatted as a time.
OpenOffice 4.1.15
Mac OS 13.5.2
Post Reply