oracle

Thursday, 20 April 2017

SQL: Concatination Parameter. '||' Vs Concat() Function

Concatenation parameter is used to combine the result of one or more columns into a single column.

Syntax:

Select first_column ||  second_column from table_name;

Example:

Select ename||job from emp;

result:

ENAME||JOB
SMITHCLERK
ALLENSALESMAN
WARDSALESMAN
JONESMANAGER
MARTINSALESMAN
BLAKEMANAGER
CLARKMANAGER
SCOTTANALYST
KINGPRESIDENT
TURNERSALESMAN
ADAMSCLERK
JAMESCLERK
FORDANALYST
MILLERCLERK

Here is a problem as shown in result , there is no space between the employee name and job.
We can  fix it by using a space between parameters.

Query:

Select ename||' '||job from emp;

Resutl:

Ename||' '||job
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
JONES MANAGER
MARTIN SALESMAN
BLAKE MANAGER
CLARK MANAGER
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK

Concat() Function:

Concat function is also used to combine the result of  two columns.

Syntax:
Select concat(first_column, Second_column) from table_name;

Example:
Select concat(ename, job) from emp;
Result:

concat(ename,job)
SMITHCLERK
ALLENSALESMAN
WARDSALESMAN
JONESMANAGER
MARTINSALESMAN
BLAKEMANAGER
CLARKMANAGER
SCOTTANALYST
KINGPRESIDENT
TURNERSALESMAN
ADAMSCLERK
JAMESCLERK
FORDANALYST
MILLERCLERK

And if we want to create a space between the employee name and the job. then we use anothor concatenation function.

Example:
select concat(concat(ename,' '),job) from emp;
Result:

concat(concat(ename, ' '), job) from emp;
SMITH CLERK
ALLEN SALESMAN
WARD SALESMAN
JONES MANAGER
MARTIN SALESMAN
BLAKE MANAGER
CLARK MANAGER
SCOTT ANALYST
KING PRESIDENT
TURNER SALESMAN
ADAMS CLERK
JAMES CLERK
FORD ANALYST
MILLER CLERK

The use of concatenation function is complicated.


No comments:

Post a Comment