Dominic Fichera Dominic Fichera - 5 months ago 6
SQL Question

String Replace a joined SQL SELECT statement

I'm wondering if it is possibly to complete a string replace on a joined SQL statement. For example, my query is:

SELECT (SITE_NAME || ', ' || LOT_NUMBER || ' ' || UNIT_TYPE || ' ' || LEVEL_NUMBER
|| ' ' || UNIT_NUMBER || ' ' || ROAD_NUMBER_1 || ' ' || ROAD_NUMBER_2 || ' ' || ROAD_NAME
|| ' ' || ROAD_TYPE || ' ' || ROAD_SUFFIX || ', ' || SUBURB || ', ' || STATE) AS address
FROM ADDRESS_LOOKUP_TOOL
WHERE ADD_ID = :P1_ADD_ID;


This statement works perfectly.... providing every single address field is populated. If only some sections are populated (i.e. there is no site name, or road suffix), there are additional commas or spaces.

Here is an example of a good select:

House of Dom, Lot 1 Suite 4 4D 119 Fake St South, Domtopia, QLD


Here is an example of a flawed select:

, Lot 1 Suite 4 4D 119 Fake St , Domtopia, QLD


Is it possibly to do a string replace on the alias where I could say, for example
replace(address, ' ,', ',')
(Where "space comma" just becomes "comma"), or is there a better way I should be structuring my select to pick this up in one go?

An additional note: This is all being completed with in Oracle's Application Express (ApEx) if this makes a difference.

I am very new to SQL, so I apologise in advance if I ask any basic follow up questions!

Thank you!

Dominic

Answer

You don't need to do it on the alias. Just do it on the expression itself.

SELECT REPLACE(
    (SITE_NAME || ', ' || LOT_NUMBER || ' ' || UNIT_TYPE || ' ' || LEVEL_NUMBER 
|| ' ' || UNIT_NUMBER || ' ' || ROAD_NUMBER_1 || ' ' || ROAD_NUMBER_2 || ' ' || ROAD_NAME 
|| ' ' || ROAD_TYPE || ' ' || ROAD_SUFFIX || ', ' || SUBURB || ', ' || STATE), 
    ' ,', ',') AS address
FROM ADDRESS_LOOKUP_TOOL
WHERE ADD_ID = :P1_ADD_ID;
Comments