DATETIME teilen durch Float

JohnML

Benutzer
Beiträge
5
Ich habe eine sqlite3 tabelle mit dem namen laufdaten1, die zwei felder enthält:

"Zeit_gelaufen", Datentyp DateTime, nur die Zeit in Form von "hh:mm:ss"
"km_gelaufen", Datentyp Numeric(5,2) also Dezimalzahl wie 1.7

Der folgende SQL code geht aber nicht:

Select Zeit_gelaufen / km_gelaufen From laufdaten1

Richtig wäre zum Beispiel:

00:11:14 / 1.7 = 00:06:36
 
Werbung:
Code:
postgres=# select '00:11:14'::interval / 1.7;
    ?column?     
-----------------
 00:06:36.470588
(1 row)

postgres=#


Hat sqllite einen Datentypen für Zeitintervalle?
 
Ich fange gerade an mit SQlite, deshalb muss ich auf deine Frage passen

Ich hab auch schon ChatGTP, Bard und Bing-chat mit dieser Frage erfolglos gequält :)
 
Ich würde sagen DATETIME in NUMERIC (bzw. in INTEGER, siehe Link) konvertieren, dann Division, dann NUMERIC in DATETIME konvertieren.

2.2. Date and Time Datatype​

SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values:

  • [...]
  • INTEGER as Unix Time, the number of seconds since 1970-01-01 00:00:00 UTC.
Applications can choose to store dates and times in any of these formats and freely convert between formats using the built-in date and time functions.
 
Thanks, i got a working solution in sqlite-forum

**** snip ****

Forum post by spindrift on 2024-01-07 09:03:03
SQLite Forum: DATETIME devide by Float

"What is wrong" is that you are trying to divide a string value by a number.

The underlying false assumption is that sqlite is thinking of your time as a numeric quantity, but it is not doing this. It is just a string.

The advice that you were given to convert this to a number of seconds is wise, as it would then be a numeric quantity and can be divided sensibly.

SQLite does have sufficient datetime functions to achieve this, they are easily found with a simple Google search. They are slightly more difficult to use for your situation, so I will give you an example below.

```SQL
SELECT TIME((
unixepoch('00:17:35') -- convert your query to seconds
- unixepoch ('00:00:00') -- remove basis to give absolute value
) / 7.0, -- perform division, note decimal to force float (unneeded but probably what you are expecting)
'unixepoch') -- ensure interpreted as seconds not Julian day

```

Do note that times must be rigidly in HH:MM or HH:MM:SS format with a leading zero where needed.

Your initial query therefore would be:


SELECT TIME((unixepoch('00:11:14')-unixepoch('00:00:00'))/1.7,'unixepoch');
:
**** snip ****


Next problem:

A Gui-App with freepascal ( = Delphi ) in a DBGrid allweys displays SQLITE type TEXT as TMemo

I hope i can find a solution for that too ...
 
Soweit ich weiß, gibt es seit einiger Zeit bei SQLite eine Strict Option, die u.a. dafür sorgt, dass die Typdefinitionen der Tabellenspalten eingehalten werden. Hab ich noch nicht benutzt, aber ist sicher ein Blick wert, vielleicht sogar sehr empfehlenswert.
Vielleicht hilft es aber auch nicht, sonst würde man sowas wohl in einem SQLite Forum auch erfahren, wissen, weitergeben.

Allgemein kann man zu SQLite sagen, dass es gerade aufgrund der "type affinity" kein ideales Lernsystem ist. Die Umgang mit Typen ist sehr minimalistisch und erzeugt eine Menge unerwünschter Nebeneffekte.

Wenn es also neben der "gefühlten" Einfachheit von SQLite keinen expliziten Grund gibt, warum es SQLite sein muss, lieber eine richtige DB nehmen. Damit lernt es sich besser, auch weil es tolle Dokumentation gibt.
 
A) Ich bin Rentner, also jede Menge Zeit zum basteln

B) Eigentlich wollte ich ja ZMSql nutzen. Das blöde Ding funktioniert zwei mal und dann sind die Daten futsch, sehr schlecht :-(

C) Die nächste Alternative war halt SqLite .. mit andren Poblemen .... aber ich mach da weiter, habe ja auch noch eine funktionierende Lösung in google Tabellen

Weiter gehts ...
 
Werbung:
Zurück
Oben