[Solved] Query - Type of empty column

Creating tables and queries
Post Reply
Hypno
Posts: 21
Joined: Tue Jun 14, 2022 12:47 pm

[Solved] Query - Type of empty column

Post by Hypno »

I want to make a union of several tables and other SQL queries, there are date fields in these tables. However, some parts from this union have blank fields created because there are no dates in some parts. When I do a union of those queries where the fields have dates it's OK, when I do a union separately where there are no dates it's also OK, but I can't union both those with dates and those without. I get a type mismatch error. Is it possible to force an empty field in a query to be typed as a date? Something like
SELECT '' as "Date1" as DateType from "table1"

Something like null would be nice but for the date type something like NullDate
Last edited by Hypno on Thu Jun 22, 2023 10:35 am, edited 1 time in total.
LibreOffice 7.2.2.1 (x64), Windows 10
Hypno
Posts: 21
Joined: Tue Jun 14, 2022 12:47 pm

Re: Query - type of empty column

Post by Hypno »

I solved the problem by creating a table with one record containing a blank date field and appending this table to the query. The solution works but is light years away from elegance and common sense.
More and more I come to the conclusion that OpenOffice Base is for masochists.
LibreOffice 7.2.2.1 (x64), Windows 10
eremmel
Posts: 1080
Joined: Tue Dec 30, 2008 1:15 am

Re: Query - type of empty column

Post by eremmel »

Hypno wrote: Thu May 25, 2023 2:28 pm More and more I come to the conclusion that OpenOffice Base is for masochists.
OO Base is just a front end to a database. By default it uses a rather old database (HSQLDB 1.8) internally. You are free to connect to a external database of your choice. An external database will have for sure better capabilities.

Most of time there are many solutions for a problem. My experience is that my own knowledge is most of the time the limiting factor not a tool.
It's Microsoft marketing that tells you computers are qualified for non-technicians
W11 22H2 (build 22621), LO 7.4.2.3(x64)
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: Query - type of empty column

Post by keme »

Not sure exactly what causes your issue, but I think UNION should not present data type issues with identically named (or labeled) fields, unless you have group operations or use your query to update other tables. With more detail provided, the cause/solution may dawn upon us ;-)

Have you looked into COALESCE? That may be relevant if intermittent null values cause error.

I guess it is not supported with AOO Base "embedded" (old HSQLDB version), but your sig says LibreOffice. I think all the "native" LO DB drivers support COALESCE. All current standalone DB services should also work fine.
 Edit: Memory failed me. I stand corrected. Thanks Charlie.it! 
Last edited by keme on Wed May 31, 2023 12:43 pm, edited 1 time in total.
User avatar
charlie.it
Volunteer
Posts: 417
Joined: Wed Aug 21, 2013 2:12 pm
Location: Italy

Re: Query - type of empty column

Post by charlie.it »

keme wrote: Wed May 31, 2023 11:57 am I guess it is not supported with AOO Base "embedded" (old HSQLDB version) ...
COALESCE is supported in the embedded HSQLDB 1.8.
charlie
Italian AOO Admin
macOS 14 Sonoma M1: Open Office 4.1.15 - LibreOffice 7.5.7.1

http://www.charlieopenoffice.altervista.org
User avatar
keme
Volunteer
Posts: 3704
Joined: Wed Nov 28, 2007 10:27 am
Location: Egersund, Norway

Re: Query - type of empty column

Post by keme »

SQL explicit typecasting sprang to mind. If you are having data type issues, this may be what you are looking for.
https://www.sqlite.org/lang_expr.html#castexpr
dreamquartz
Posts: 881
Joined: Mon May 30, 2011 4:02 am

Re: Query - Type of empty column

Post by dreamquartz »

Hello,

If a record must be part of a UNION ALL, but is not part of a certain table/view or it is not needed/wanted in a UNION ALL, it can be excluded, but it still needs to be identified in the UNION ALL, because all parts of the UNION must be equal.
An example is in the top part of the UNION ALL, where I am using NULL "NickName".
"NickName" is a valid record, but I did not want/need it in the top part for trying to find 'double' entries in the table "tPerson". I did need it in the bottom part.

As indicated by others, all the records must be of the same type to prevent mismatches as well.

Code: Select all

SELECT
	 "a"."PersonID",
	 CASE WHEN
		 "a"."GivenName" IS NULL
		 THEN
			 COALESCE ( "a"."Surname", '' )
		 ELSE
			 COALESCE ( "a"."Surname" || ',', '' )
	 END ||
		 CASE WHEN
			 "a"."Surname" IS NULL
				 THEN
					 COALESCE ( "a"."GivenName", '' )
				 ELSE
					 COALESCE ( ' ' || "a"."GivenName", '' )
		 END "Person",
	 "a"."Surname",
	 "a"."GivenName",
	 NULL "NickName",
	 NULL "DateOfBirth",
	 COUNT( * ) "Rank"
 FROM
	 "tPerson" "a"
		 LEFT JOIN "tPerson" "b" ON
			 "a"."PersonID" >= "b"."PersonID" AND
			 "a"."Surname" = "b"."Surname" AND
			 "a"."GivenName" = "b"."GivenName"
 GROUP BY
	 "a"."Surname",
	 "a"."GivenName",
	 "a"."Nickname",
	 "a"."DateOfBirth",
	 "a"."PersonID"
UNION ALL
SELECT
	 "a"."PersonID",
	 CASE WHEN
		 "a"."Nickname" IS NULL
		 THEN
			 COALESCE ( "a"."Surname", '' )
		 ELSE
			 COALESCE ( "a"."Surname" || ',', '' )
	 END ||
		 CASE WHEN
			 "a"."Surname" IS NULL
				 THEN
					 COALESCE ( "a"."Nickname", '' )
				 ELSE
					 COALESCE ( ' ' || "a"."Nickname", '' )
		 END "Person",
	 "a"."Surname",
	 NULL "GivenName",
	 "a"."Nickname",
	 NULL "DateOfBirth",
	 COUNT( * ) "Rank"
 FROM
	 "tPerson" "a"
		 LEFT JOIN "tPerson" "b" ON
			 "a"."PersonID" >= "b"."PersonID" AND
			 "a"."Surname" = "b"."Surname" AND
			 "a"."GivenName" = "b"."Nickname"
GROUP BY
	 "a"."Surname",
	 "a"."GivenName",
	 "a"."Nickname",
	 "a"."DateOfBirth",
	 "a"."PersonID"
Hopes this helps as well

Dream
LO 7.x, HSQLDB 2.7.x & Ubuntu 22.04 LTS.
Cazer
Posts: 53
Joined: Mon May 15, 2023 11:55 am

Re: Query - Type of empty column

Post by Cazer »

Good to hear you found a workaround, even if it's not the most elegant solution! As dreamquartz suggested, using 'UNION ALL' and 'NULL' placeholders for fields that are not part of certain queries could be a cleaner alternative. It would let you keep the structure of your UNION intact, without resorting to extra tables.

Also, 'COALESCE' is a good tool to remember for handling NULL values. It returns the first non-null value in a list, which can be useful when dealing with intermittent null values.
OpenOffice 4.1.14
OS
Post Reply