CONCAT in Oracle SQL Developer - sql

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

Related

Concate columns using oracle 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

select concat(first_name,' ',last_name) as "full name "from employees ---how to bring space when 2 columns are concatenated in oracle---

select concat(first_name,' ',last_name) as "full name "from employees
Oracle's CONCAT function takes only two arguments. So if you wanted to continue along with your exact current approach, you would have to chain together two calls to CONCAT:
SELECT CONCAT(CONCAT(first_name, ' '), last_name) AS "full name"
FROM employees;
Or, you could just use the ANSI standard concatenation operator ||:
SELECT first_name || ' ' || last_name AS "full name"
FROM employees;
Use the operator ||:
select (first_name || ' ' || last_name) as full_name
from employees
Check the below using ||
select first_name || ' ' || last_name as "full name" from employees;
If you want to use CONCAT operator you can try the below:
SELECT CONCAT(CONCAT(first_name , ' '),LAST_NAME) AS "full name"
FROM employees;

Concat firstname and lastname with space in between in oracle pl sql

I have one requirement to concat user first_ name, and last_name with space in between in Oracle.
Ex: first_name is 'Hopkins' and last_name is 'Joe'.
Full name should be printed as Hopkins Joe.
I'm using Oracle 11g and it is working in SQL query, but not working in stored procedure.
Try this:
SELECT CONCAT(CONCAT(first_name, ' '),last_name)
OR
SELECT first_name || ' ' || last_namefrom;
use this:
TRIM(FIRST_NAME || ' ' || LAST_NAME)
if any of first_name or last_name is blank or null the extra space we are adding will be trimmed.
Try this
select first_name || ' ' || last_name as full_name from table
Example:
SELECT 'Dave' || ' ' || 'Anderson' as full_name
FROM table;
Result: 'Dave Anderson'
No need to use CONCAT function twice. Concat with space will work in this way
SELECT CONCAT(first_name,(' '||last_name)) AS full_name
This will work:
select first_name||' '||last_name
from table_name
where first_name is not null -- "if the first_name can be null"
and last_name is not null -- "if the last_name can be null"
;

Selecting form with SUBSTR

I have a table with name and profession I need to select the name of all employees with the first letter of their profession surrounded by () to be like:
Sara (D)
Jack (E)
Use || to concatenate strings and SUBSTR() to get the first character:
SELECT name || ' (' || SUBSTR( profession, 1,, 1 ) || ')'
FROM your_table;

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