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:
CREATE VIEW ClientsAddressList(Client_ID, FirstName, LastName, ResidenceAddress, City, PostalCode, Province) AS
SELECT Client_ID,
FirstName,
LastName,
SPLIT_PART(Address, '-', 1) AS ResidenceAddress,
SPLIT_PART(Address, '-', 2) AS City,
SPLIT_PART(Address, '-', 3) AS PostalCode,
SPLIT_PART(Address, '-', 4) AS Province
FROM Clients;
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:
SELECT Client_ID, FirstName, LastName
FROM ClientsAddressList
WHERE City = 'Florence'
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:
SELECT Client_ID, FirstName, LastName, City
FROM ClientsAddressList
WHERE Client_ID = '00451'
| 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:
SELECT Client_ID
FROM ClientAddressList
WHERE PostalCode LIKE = '%123'
| Client_ID |
|---|
| 00451 |
Can someone explain me what could be the problem, please? Thank you so much!
'Florence'but' Florence 'for instance. Why don't you just normalize the original table though?'Via dei Banchi 45-Florence-50123-Florence'the problem could be solved. But what do you mean by 'normalize the original table'? I'm sorry, I am a newbie with DBMS systems