Skip to content
Advertisement

PostgreSQL: `VIEW` returns no rows with `SPLIT_PART()` columns

Problem Description:

Hi everyone, I’m having some troubles querying on a VIEW whose columns are, in part, the result of SPLIT_PART() function on og table’s column; I created the VIEW as it follows:

My intention was to divide the structured attribute (Clients.Address defined as a string VARCHAR(255)) which contains all the informations releated to client’s domicile in several columns to separately query (e.g. SELECT FirstName, LastName FROM ClientAddressList WHERE City LIKE 'N%'; or SELECT Client_ID FROM ClientAddressList WHERE PostalCode = '82305';).

What I experience:

The Clients table contains one test row:

Client_ID FirstName LastName ResidenceAddress City PostalCode Province
00451 Ezio Auditore Via dei Banchi 45 – Florence – 50123 – Florence Florence 50123 Florence

So my VIEW has this row:

Client_ID FirstName LastName ResidenceAddress City PostalCode Province
00451 Ezio Auditore Via dei Banchi 45 Florence 50123 Florence

I’ve tried:

And it returns no result:

Client_ID FirstName LastName ResidenceAddress City PostalCode Province

But if I query on columns that are not the result of SPLIT_PART() it works:

Client_ID FirstName LastName City
00451 Ezio Auditore Florence

What I expect:

I would WHERE clause to work and returns values even on SPLIT_PART() result columns:

Client_ID
00451

Can someone explain me what could be the problem, please? Thank you so much!

Advertisement

Answer

As sticky bit wrote: there are spaces around the values. There are two ways to deal with this. One way is to just slap a trim() around the expressions in the view:

The other option is to use an appropriate regex to split the information to remove the whitespace during splitting:

Online example


In the long run you should fix your data model by properly normalizing it and storing those values in separate columns. I don’t know how many city names contain dashes in Italy, but in Germany, this pattern would break quickly with city names like “Garmisch-Partenkirchen” or “Leinfelden-Echterdingen”

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement