Frequently asked questions - Base

    From The Document Foundation Wiki
    < Faq‎ | Base
    This page is a translated version of the page Faq/Base/125 and the translation is 14% complete.


    Wie muss ich mit leeren Feldern umgehen?

    Bei der Verbindung von Feldern kann es passieren, dass das Ergebnis unerwartet ein leeres Feld ergibt. Hier gilt grundsätzlich: Ein Feld ohne Inhalt in einer Datenbank hat die Eigenschaft NULL. Dies ist nicht gleichbedeutend mit einem leeren Text, also z.B. der Eingabe ''. Dies ist auch nicht gleichbedeutend mit 0 oder einem Markierfeld, das extra abgewählt wurde, also 0 oder FALSE ist. Wird nun mit einem Feld ohne Inhalt ein anderes Feld verknüpft, so ist das Ergebnis immer NULL.

    Vorname Nachname "Nachname"||', '||"Vorname"
    Rolf Hautdenlukas Hautdenlukas, Rolf
    Annette NULL --kein zusammengesetzter Name--
    NULL Trumpel --kein zusammengesetzter Name--
    Zahl1 Zahl2 Zahl1 + Zahl2
    2 5 7
    2 NULL --keine Summe--
    NULL 5 --keine Summe--

    Um zu vermeiden, dass in den obigen Fällen die jeweiligen Ergebnisspalten leer bleiben, gibt es für die HSQLDB die Funktion IFNULL. Universeller hingegen ist die Funktion COALESCE, die sowohl bei der HSQLDB als auch in Firebird funktioniert:

    Vorname Nachname COALESCE("Nachname", '???')||COALESCE(', '||"Vorname",'')
    Rolf Hautdenlukas Hautdenlukas, Rolf
    Annette NULL ???, Annette
    NULL Trumpel Trumpel
    Zahl1 Zahl2 COALESCE(Zahl1, 0) + COALESCE(Zahl2, 0)
    2 5 7
    2 NULL 2
    NULL 5 5
    "Total" - "Account"

    will give the result Null' if one of the fields hasn't been entered. The solution is to use a function that tests whether fields contain data or not. Amongst the functions available IFNULL' is often used. Its syntax is

    IFNULL(exp, value)

    If exp is Null', value is returned, otherwise the value of exp is returned.

    For this example, IFNULL is used to return zero when the field is empty.

    The first calculation results in null (incorrectly shown) for row "1" because "Acompte" has no value

    The SQL command is

    SELECT "ID", "Acompte", "Total", "Total" - "Acompte", IFNULL( "Total", 0 ) - IFNULL( "Acompte", 0 ) FROM "Table1"

    Selecting rows with null values

    Selecting rows containing empty fields.

    • The selection criterion to use in draft mode is :


    IS EMPTY


    • The selection criterion in SQL mode is


    IS NULL


    • For the example above

    Selecting rows with empty values


    Notes

    1. In draft mode it is also possible to enter the criterion IS NULL. After validation it will automatically be replaced by IS EMPTY
    2. In SQL mode only the syntax IS NULL is valid


    SELECT "ID", "Acompte", "Total" FROM "Table1" WHERE "Acompte" IS NULL


    Managing boolean fields


    The possible states of boolean fields
    • The selection of rows having unfilled boolean fields uses the same criterion, IS EMPTY.
    • It should be noted that a boolean field can have three states: checked, unchecked and empty.

    The two latter states are not equivalent: unchecked = FALSE (false or 0), and empty = indeterminate/unspecified.

    • LibreOffice shows these different states as follows: unchecked = blank, unfilled = background colour
      • IS EMPTY selects unfilled cases (not entered)
      • FALSE select unchecked (value is false or 0)
      • TRUE selects checked (value is true or 1)