So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".
Related
I'm having trouble formatting this result set. The columns vendor_address1 and vendor_address2 have the text NULL in them. I need to format the output like the picture. I have tried various case statements, the replace() function, etc. I initially thought the NULL text was a null value but then soon realized its actual text. The Pacific Bell vendor should have no spaces in the beginning. I was trying to remove the spaces with the LTRIM function (or that was my idea).
Here is my code:
SELECT vendor_name,
LTRIM(REPLACE(vendor_address1, 'NULL', ' ')) || ', ' ||
LTRIM(REPLACE(vendor_address2, 'NULL', ' ')) || ', ' ||
vendor_city || ', ' || vendor_state || ' ' || vendor_zip_code
AS "Complete Address"
FROM ap.vendors
WHERE vendor_name LIKE 'P%'
My Current Output
My Desired Output
Vendors Table
EDIT: I tried the suggestion below but got this result instead:
EDIT2: So I was able to figure it out. Here is the code I ended up using, in case anyone was wondering:
SELECT vendor_name,
DECODE(vendor_address1, 'NULL', '', vendor_address1 || ', ') ||
DECODE(vendor_address2, 'NULL', '', vendor_address2 || ', ') ||
vendor_city || ', ' ||vendor_state || ' ' || vendor_zip_code AS
"Complete Address"
FROM ap.vendors
WHERE vendor_name LIKE 'P%'
This gives the output that matches my "Desired Output"
What you need is NVL2() function as you need non-null case such as
SELECT vendor_name,
NVL2(TRIM(vendor_address1), TRIM(vendor_address1)||', ',null )||
NVL2(TRIM(vendor_address2), TRIM(vendor_address2)||', ',null )||
vendor_city || ', ' ||vendor_state || ' ' || vendor_zip_code AS "Complete Address"
FROM ap.vendors
WHERE vendor_name LIKE 'P%'
where vendor_city,vendor_state and vendor_zip_code columns are presumed to be non-null always. The vendor_addresses should be suffixed with a comma if either of them is non-null.
Btw, 'NULL' is just a literal, but not identical to null(or NULL)
Demo
I have a simple Select query that aggregates one column containing large texts.
The following worked for me with small texts but I am now exceeding the Listagg character limit (4000 bytes ?).
I am very new to Oracle and couldn't find a proper solution for this online that I could apply here.
Can someone tell me the best alternative to this ?
My Query (simplified):
SELECT
m.S_ID AS SID
, LISTAGG
(
'ITEM NO.: ' || m.ITEM ||
' -nl-ARTICLE: ' || a.ARTICLE ||
' -nl-NET: ' || m.NET ||
' -nl-TAX: ' || NVL(m.TAX, 0) ||
' -nl-GROSS: ' || (m.NET + m.TAX),
' -nl--nl-'
) WITHIN GROUP (ORDER BY m.S_ID) AS Details
/* ... */
FROM
myTable m
/* ... */
Many thanks for any help with this,
Mike
One of possible method.
select xmlagg(xmlelement(xxx,'ITEM NO.: ' || m.ITEM ||
' -nl-ARTICLE: ' || a.ARTICLE ||
' -nl-NET: ' || m.NET ||
' -nl-TAX: ' || NVL(m.TAX, 0) ||
' -nl-GROSS: ' || (m.NET + m.TAX),
' -nl--nl-'||',<-separator').extract('//text()') order m.S_ID).getClobval() from mytable
group by ...
2nd method.
oracle allows to creat own aggregation function user defined aggregation function
How can I add blank spaces between the columns?
I want to display 3 columns in one column.
select artikel_nr || preis || termin from auftragsposition
Just concatenate in a space as a text literal:
select artikel_nr || ' ' || preis || ' ' || termin from auftragsposition
So I basically wanna display this (whole row in ONE column):
I like [type column] cake with [icing column] and a [fruit column].
The result should be:
Cake_Column
----------------
I like chocolate cake with whipped_cream and a cherry.
I like strawberry cake with vanilla_cream and a lemon_slice.
etc.
etc.
I need some sort of TO_CHAR statement that does ([column] "some text" [column]) "new_column_name";
What am I supposed to know?
You have two options for concatenating strings in Oracle:
CONCAT
Using ||
CONCAT example:
CONCAT(
CONCAT(
CONCAT(
CONCAT(
CONCAT('I like ', t.type_desc_column),
' cake with '),
t.icing_desc_column),
' and a '),
t.fruit_desc_column)
Using || example:
'I like ' || t.type_desc_column || ' cake with ' || t.icing_desc_column || ' and a ' || t.fruit_desc_column
Did you try the || operator ?
Concatenation Operator Documentation from Oracle >>>
select 'i like' || type_column || ' with' ect....
Below query works for me #Oracle 10G ----
select PHONE, CONTACT, (ADDR1 || '-' || ADDR2 || '-' || ADDR3) as Address
from CUSTOMER_DETAILS
where Code='341';
O/P -
1111 abc#gmail.com 4th street-capetown-sa
The Oracle/PLSQL CONCAT function allows to concatenate two strings together.
CONCAT( string1, string2 )
string1
The first string to concatenate.
string2
The second string to concatenate.
E.g.
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake FROM table;
Try this:
SELECT 'I like ' || type_column_name || ' cake with ' ||
icing_column_name || ' and a ' fruit_column_name || '.'
AS Cake_Column FROM your_table_name;
It should concatenate all that data as a single column entry named "Cake_Column".
I have a set of table as following
customer(cus_id,cus_first_name,cus_last_name);
insert into customer values ('c001', 'tan', 'wah khang');
I want to create a select statement to display the first name join with the last name.
Example :
tan wah khang
is that possible?
You can use the (this is not called "join" but) concatenation *|| (double pipe)* operator:
SELECT (cus_first_name || ' ' || cus_last_name) AS full_name
FROM customer
|| isn't quite equivalent to MySQL's CONCAT_WS. CONCAT_WS eliminates the delimiter if one of the operands is NULL. So if firstname is NULL and lastname is 'Smith', in MySQL:
CONCAT_WS(' ', firstname, lastname) returns "Smith"
whereas, in Oracle:
firstname || ' ' || lastname returns " Smith" (prepended with a space)
I'd love to know if there's a true equivalent, or if you'd have to write a stored procedure to emulate CONCAT_WS. It's terribly useful.
select cus_first_name || ' ' || cus_last_name from customer
Yes:
select cus_first || ' ' || cus_last from your_table;
In Oracle, PostgreSQL, DB2, Informix:
select cus_first_name || ' ' || cus_last_name from customer
In SQL-Server:
select cus_first_name + ' ' + cus_last_name from customer
In MS-Access:
select cus_first_name & ' ' & cus_last_name from customer
In MySQL:
select concat(cus_first_name , ' ', cus_last_name) from customer
In Informix:
select concatenate(cus_first_name,concatenate(' ',cus_last_name)) from customer