Concate columns using oracle SQL - sql

C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3
pa.address_line_1,
pa.address_line_2,
pa.address_line_3 from per_addresses_f pa ;
how to join three column as single address column with oracle SQL concatenate
How to write query for joining 3 columns as single column?

You can write the concatenation as you've written it, but you have to make sure that all the components are strings. If some of them isn't, put it in TO_CHAR function. It's also good to put an alias to your result of concatenated columns:
SELECT C.ADDRESS1 || ',' || C.ADDRESS2 || ',' || C.ADDRESS3 as concatenated_adress
FROM your_table c
EDIT : After question was edited with concrete columns that are used, table name and with desired alias stated in question columns:
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' || pa.address_line_3
as "Emergency Address"
FROM per_addresses_f pa

You can do this:
CONCAT(C.ADDRESS1, ',', C.ADDRESS2, ',', C.ADDRESS3) AS newColumnName

Try this :
SELECT firstname ||' '|| lastname AS employee_name FROM employee;

You can also try nested CONCAT assuming all address_line fields have char datatype:
SELECT CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3) AS address FROM per_addresses_f pa
But clearly pipe operator as suggested by #Goran Kutlaca is much simpler.

There are multiple ways to Concate the column in BIP.
1.
SELECT pa.address_line_1 || ',' || pa.address_line_2 || ',' ||
pa.address_line_3
as "Emergency Address" FROM per_addresses_f pa
2.
SELECT
CONCAT(CONCAT(CONCAT(pa.address_line_1,','),CONCAT(pa.address_line_2,',')),pa.address_line_3)
AS address FROM per_addresses_f pa
3.
select
CONCAT(pa.address_line_1,',',pa.address_line_2,',',pa.address_line_3)
as address FROM per_addresses_f pa

Related

CONCAT in Oracle SQL Developer

I don't know if I should use comma (,) or "||" as separator in CONCAT function for Oracle SQL.
This one works:
SELECT CONCAT(first_name, last_name) as name
FROM TABLE1
However, none of these below works out (I need to put a space between first_name and last_name, or to extract the initial letter from last_name and wrap this letter with parenthesis):
SELECT CONCAT(first_name || last_name) as name
FROM TABLE1
SELECT CONCAT(first_name || ' ' || last_name) as name
FROM TABLE1
SELECT CONCAT(first_name, ' ', last_name) as name
FROM TABLE1
SELECT CONCAT(first_name, '(', UPPER(STR(last_name, 1,1)), ')') as name
FROM TABLE1
Using double-pipe (||) instead of CONCAT in Oracle SQL is a more efficient way to go. Thanks to the comments that folks provided in here.
SELECT (first_name || ' ' || last_name)as name
FROM Table1;
Output:
John Smith
I also fixed the last script in the above question. Somehow it needs two right parentheses before "as name".
SELECT first_name || '(' || UPPER(SUBSTR(last_name, 1,1)) || ')')) as name, first_name, last_name
FROM TABLE1
Output:
John(S) John Smith

comma separated list oracle sql

I have a list of comma separated values in a single field and need to find all rows which meet a certain criteria. The code I am using is:
select * from table where column_name in
(
select regexp_substr('A+','A-','aabn','[^,]+', 1,level) from table
connect by regexp_substr('A+','A-','aabn', '[^,]+', 1,level) is not null
);
so I want to find all the rows which contain A+, A- and aabn
Why not just use a single regular expression?
where regexp_like(column_name, '(^|,)(A[+]|A[-]|aabn)($|,)')
Here is a DB<>fiddle.
Also, as powerful as regular expressions are, you should not be storing multiple values in a single string. Oracle has many other options for storing multiple values, including the traditional association/junction table.
You don't need regular expressions, just use LIKE to check that the value you require is a sub-string of the column (both surrounded by delimiter characters):
If you want all 3 in the list then use AND:
SELECT *
FROM table_name
WHERE ',' || column_name || ',' LIKE '%,A+,%'
AND ',' || column_name || ',' LIKE '%,A-,%'
AND ',' || column_name || ',' LIKE '%,aabn,%'
If you want any 1 in the list then use OR:
SELECT *
FROM table_name
WHERE ',' || column_name || ',' LIKE '%,A+,%'
OR ',' || column_name || ',' LIKE '%,A-,%'
OR ',' || column_name || ',' LIKE '%,aabn,%'

IN clause not working in oracle

I have a query which uses IN clause and it is not working for below case:
Select *
from table1
where
Rollno || '/' || UserId IN ('1/001,2/002')
It is not working because you haven't wrapped each value in single quotes ' :
SELECT *
FROM table1
WHERE Rollno || '/' || UserId IN ('1/001','2/002')
Notulysses has the right syntax for in. But, if you have to deal with a string, you can rephrase this as like:
where ',' || Rollno || '/' || UserId || ',' like '%,' || '1/001,2/002' || ',%'
like is a better approach. Sometimes in the real world, you might have to deal with comma-delimited strings.

format column headers during concat,oracle

I need to format column headers in the output of sql while using concat
Eg:
SELECT '' || to_char(sysdate,'ddmmyyyy') as DATE || ',' || ENO|| ',' || NAME|| ''
FROM EMP;
would retrieve me
ORA-00923: FROM keyword not found where expected.
Need the output as:
DATE ENO NAME
-----------------
251013 7560 RAM
251013 7561 ROSS
This format works
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME"
FROM EMP
but I have an issue with
ORA-00972: identifier is too long
when the length of column names inside as "" exceeds 30 characters
Eg:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME ||
',' || EMPLOYEE_IDENTIFICATION_NUMBER as "DATE,ENO,NAME,EMPLOYEE_IDENTIFICATION_NUMBER"
FROM EMP;
To achieve this output you have to build your query like this
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as "DATE,ENO,NAME" FROM EMP
You need to move the alias, if you really need it, at the end of the SELECT clause. Also the empty strings ('') can be removed:
SELECT to_char(sysdate,'ddmmyyyy') || ',' || ENO || ',' || NAME as DATE FROM EMP;

SQL join 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