String Replace a joined SQL SELECT statement - sql

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

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;

Related

Adding spaces to a piped concatenation

How do I change my select statement so that the following will return spaces between the concatenated columns?
The current query is similar to:
SELECT
address_line1 || address_line2 || address_line3 || address_line4 || city || state || county || province || country || zip as address
FROM
table
Many thanks
Just add space characters in between the fields:
SELECT
address_line1 || ' ' || address_line2 || ' ' || address_line3 || ' ' ||
address_line4 || ' ' || city || ' ' || state || ' ' || county || ' ' ||
province || ' ' || country || ' ' || zip AS address
FROM yourTable;

Why keeps showing this error: ORA-00936: missing expression

CREATE OR REPLACE FORCE EDITIONABLE VIEW "VU_REPORT5" ("Selling Report") AS
SELECT a2.FIRST_NAME || a2.SUR_NAME ||', living in ' || a4.COUNTRY ||
', ' || a4.CITY || ' ' || a4.LINE_1 || a4.LINE_2 ||
a4.LINE_3 || a4.LINE_4 || ', bought an ' || a1.MAKE || ' ' ||
a1.MODEL || ' from employee ' || a3.FIRST_NAME || ' ' ||
a3.SUR_NAME || ' at ' || a1.SOLD_DATE || ', Making a profit of ' ||
to_char(a1.SOLD_PRICE - a1.PURCHASE_PRICE) ||' pounds.' AS "Selling Report",
to_char(SELECT sum(a5.SOLD_PRICE)-sum(a5.PURCHASE_PRICE)
FROM CAR a5
where (to_date(a5.SOLD_DATE,'mm-dd-yyyy') <= to_date(a1.SOLD_DATE,'mm-dd-yyyy'))
ORDER BY a5.SOLD_DATE
GROUP BY a5.SOLD_DATE) AS "OVERALL Report"
FROM CAR a1,
CUSTOMER a2,
staff a3,
ADDRESS a4
WHERE a1.BOUGHT_BY_CUSTOMER_NO = a2.CUSTOMER_NO and
a1.SOLD_BY_STAFF_NO = a3.STAFF_NO and
a4.ADDRESS_NO = a2.ADDRESS_NO
ORDER BY a1.SOLD_DATE
One issue is that the view is defined as returning a single column ("Selling Report"), but the query actually returns two columns ("Selling Report" and "OVERALL Report").
Another issue is that you can't put a sub-select into a function call; in this case
to_char(SELECT sum(a5.SOLD_PRICE)-sum(a5.PURCHASE_PRICE)
FROM CAR a5
where (to_date(a5.SOLD_DATE,'mm-dd-yyyy') <= to_date(a1.SOLD_DATE,'mm-dd-yyyy'))
ORDER BY a5.SOLD_DATE
GROUP BY a5.SOLD_DATE)
simply isn't valid. I'm not sure what you're trying to do with this sub-query so I can't really advise you on how to correct the problem.
Best of luck.

How to Select 2 Different Names from same table and display them on the Condition

I have this Query
SELECT NAME_NO
,(
SELECT FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE
FROM NAMES
WHERE NAME_NO = 1
) AS "NAME1: NAME, DOB, PHONE"
,(
SELECT FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE
FROM NAMES
WHERE NAME_NO = 2
) AS "NAME2: NAME, DOB, PHONE"
,
FROM NAMES;
I get this error:
01427. 00000 - "single-row subquery returns more than one row"
I need multiple records.
What is the best method to solve this?
Try this one:
SELECT NAME_NO, FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE AS "NAME1: NAME, DOB, PHONE"
FROM NAMES
UNION
SELECT FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE AS "NAME2: NAME, DOB, PHONE"
FROM NAMES
WHERE NAME_NO = 2
or this one:
WITH N1 AS (
SELECT NAME_NO,FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE AS "VAL"
FROM NAMES
WHERE NAME_NO = 1),
N2 AS (
SELECT FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE AS "VAL"
FROM NAMES
WHERE NAME_NO = 2)
SELECT
NAME_NO,VAL
FROM N1,N2;
You need to use PIVOT. Try this
Select A "NAME1: NAME, DOB, PHONE" , B "NAME2: NAME, DOB, PHONE" from (SELECT FNAME || ' ' || LNAME || ' ' || BIRTH_DT || ' ' || ' ' || PHONE N, NAME_NO
FROM NAMES)
Pivot
(Max(N) for NAME_NO in (1 as A, 2 as B)
);

Formatted Input in Table in plsql

I want to input the following things -
ACCEPT p_cname PROMPT 'Enter Customer Name: '
ACCEPT p_cyear PROMPT 'Enter Car Year: '
ACCEPT p_color PROMPT 'Enter Car Color: '
ACCEPT p_make PROMPT 'Enter Car Make: '
ACCEPT p_model PROMPT 'Enter Car Model: '
ACCEPT p_trim PROMPT 'Enter Car Trim: '
ACCEPT p_enginetype PROMPT 'Enter Car Engine Type: '
ACCEPT p_option PROMPT 'Enter Option Name: '
ACCEPT p_ocode PROMPT 'Enter Option Code: '
then add it in table as
NAME - WANT
Name - year color model trim enginetype 'w/' Option ( ocode )
I tried to format it using -
INSERT INTO table
VALUES ('&p_cname', '&p_cyear' || ' ' || '&p_color' || ' ' || '&p_make' || ' ' || '&p_model' ||
|| ' ' || '&p_trim' || ' ' || '&p_enginetype' || ' ' || '&p_option' || '(' || '&p_ocode' || ')');
BUT IT DOED NOT WORK.
The error you pasted into comments doesn't correspond to the code in your question. If I execute the code I get:
SQL> #test
Enter Customer Name: a
Enter Car Year: b
Enter Car Color: c
Enter Car Make: d
Enter Car Model: e
Enter Car Trim: f
Enter Car Engine Type: g
Enter Option Name: h
Enter Option Code: i
old 2: VALUES ('&p_cname', '&p_cyear' || ' ' || '&p_color' || ' ' || '&p_make' || ' ' || '&p_model' ||
new 2: VALUES ('a', 'b' || ' ' || 'c' || ' ' || 'd' || ' ' || 'e' ||
old 3: || ' ' || '&p_trim' || ' ' || '&p_enginetype' || ' ' || '&p_option' || '(' || '&p_ocode' || ')')
new 3: || ' ' || 'f' || ' ' || 'g' || ' ' || 'h' || '(' || 'i' || ')')
|| ' ' || 'f' || ' ' || 'g' || ' ' || 'h' || '(' || 'i' || ')')
*
ERROR at line 3:
ORA-00936: missing expression
SQL>
That's because you have two concatenation operators missing it's second operand:
'&p_model' || || ' ' || '&p_trim'
Eliminate the extra operator and the insert works:
'&p_model' || ' ' || '&p_trim'

How would I return a salary + a 5% raise?

I am trying to return a salary + a 5% raise. I thought the following code would work but it gives me an error. Can anyone help?
SELECT last_name || ' ' || salary || ' ' ||salary*.05+salary || ' ' FROM f_staffs;
The trick is this:
SELECT last_name || ' ' || salary || ' ' || (1.05*salary) || ' ' FROM f_staffs;
are you need all in one field? if yes assuming oracle:
SELECT last_name || ' ' || to_char(salary) || ' ' ||to_char(salary*.05+salary) || ' ' FROM f_staffs;