SQL join column - sql

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

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

Reusing alias for an expression in postgresql

I am trying to do this:
select first_name || ' ' || last_name as full_name, Length(full_name) as len from customer order by len
It is not possible;
column "full_name" does not exist
So, I have to do this:
select first_name || ' ' || last_name as full_name, Length(first_name || ' ' || last_name) as len from customer order by len
Does it mean sql engine has to compute expression 'first_name || ' ' || last_name' two times?
As you observe, what you want to do is not possible. Instead, you can use a lateral join to calculate values in the FROM clause:
select v.full_name, Length(v.full_name) as len
from customer c cross join lateral
(values (first_name || ' ' || last_name)
) v(full_name)
order by len;

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"
;

How do I convert table column data from 'LastName, FirstName' to 'FirstName LastName' in Postgres

How do I convert table column data from 'LastName, FirstName' to 'FirstName LastName' in Postgres.
So if the table column contains 'Ona, Lisa' I want it to return 'Lisa Ona'. If the column doesnt contain a ' ,' then I want it to return null. I've been trying to use postgres functions SUBSTRING and REGEXP_REPLACE but cannot get anything working.
You need strpos and substr functions and a CASE to return NULL.
SELECT CASE WHEN strpos(name,', ') > 0
THEN substr(name,strpos(name,', ') + 2) ||' ' || substr(name,0, strpos(name,', '))
ELSE NULL END
FROM person
fiddle
Nevermind worked out a solution using SPLIT_PART function
SELECT
t1.sort_name,
split_part(t1.sort_name, ', ', 2)|| ' ' || split_part(t1.sort_name, ', ', 1)
FROM artist t1
WHERE
t1.sort_name LIKE '%, %'
try this
select split_part(name,',',2) ||' '|| split_part(name,',',1) as "thename" from tblname
where name LIKE '%,%'
String Functions and Operators and PostgreSQL SPLIT PART