Concat with null string

Creating tables and queries
Post Reply
rforester
Posts: 2
Joined: Fri Dec 21, 2012 9:55 pm

Concat with null string

Post by rforester »

I am trying to concatenate two fields in a query, but if one of the tow is empty, it is not returning anything.
Here is what I am using:
CONCAT( "OADDRESS1", "OADDRESS2" )

When OADDRESS2 is empty, it is returning an empty field.
How can I bring just OADDRESS1 when OADDRESS2 is empty?

Thanks!
Open Office 3.4.1 on Windows XP
User avatar
Sliderule
Volunteer
Posts: 1279
Joined: Thu Nov 29, 2007 9:46 am

Re: Concat with null string

Post by Sliderule »

Use EITHER ONE of the two statements below:

Code: Select all


CONCAT( "OADDRESS1", COALESCE("OADDRESS2",'') )


"OADDRESS1" || COALESCE("OADDRESS2",'')
Explanation: The SQL function COALESCE will return the second value ( in this case, a string with a lengh of zero :bravo: ) when the first value is NULL ( UNDEFINED ). You can read about the COALESCE function at:

http://www.hsqldb.org/doc/1.8/guide/ch09.html
http://www.hsqldb.org/doc/1.8/guide/ch09.html wrote:
COALESCE(expr1,expr2,expr3,...)

if expr1 is not null then it is returned else, expr2 is evaluated and if not null it is returned and so on
I hope this helps, please be sure to let me / us know.

Sliderule

Thanks to add [Solved] in your 1st post Subject (edit button top right) if this issue has been resolved.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Concat with null string

Post by Villeroy »

Any arithmetic or logical operation or concatenation with Null returns Null. The Coalesce function handles this by replacing Null with anything appropriate.
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
rforester
Posts: 2
Joined: Fri Dec 21, 2012 9:55 pm

Re: Concat with null string

Post by rforester »

Thanks for the reply!

It should work but now it says:

The query can not be executed. It is too complex.

Any ideas?
Open Office 3.4.1 on Windows XP
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Concat with null string

Post by FJCC »

Try setting the option Edit -> Run SQL Command Directly.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Concat with null string

Post by Villeroy »

rforester wrote:Thanks for the reply!

It should work but now it says:

The query can not be executed. It is too complex.

Any ideas?
You did not mention the type of database indicated in the status bar. COALESCE runs fine with HSQLDB.

The minimal set of functions for file based databases (dBase, spreadsheets, text etc) does not include COALESCE.
http://www.openoffice.org/dba/specifica ... tions.html
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
Post Reply